gpt4 book ai didi

python - 从一个 socket 传递到另一个 socket

转载 作者:行者123 更新时间:2023-11-28 23:03:52 24 4
gpt4 key购买 nike

我正在尝试编写一个充当中介的程序。 (男)

我只能用telnet连接:

A需要连接到M,B需要连接到M。

A通过套接字向M发送数据,M需要将数据传递给B

B在另一个套接字上向M发送数据

我通过使用共享列表启动四个线程来尝试这个

问题是它似乎没有写入另一个套接字,甚至没有接受写入。

有谁知道更好的方法来实现它并将它传递给另一个套接字

我的代码:

import sys
import arduinoReadThread
import arduinoWriteThread
import socket
class ControllerClass(object):
'''
classdocs
'''
bolt = 0
socketArray=list()

def __init__(self):
self.readAndParseArgv()
self.createThreads()

def readAndParseArgv(self):
array = sys.argv
print sys.argv
if len(array) != 3:
print "Too few arguments : ./script host:port host:port"
else:
for line in array:
if ":" in line:
splitted = line.split(':')
HOST = splitted[0]
print HOST
PORT = int(splitted[1])
print PORT
s=socket.socket(socket.AF_INET, socket.SOCK_STREAM ) #create an INET, STREAMing socket
s.bind((HOST,PORT)) #bind to that port
print "test"
s.listen(1) #listen for user input and accept 1 connection at a time.
self.socketArray.append(s)

def createThreads(self):
print "Creating Threads"
sharedArray1 = list()
sharedArray2 = list()
s1 = self.socketArray.pop()
s2 = self.socketArray.pop()
sT1 = arduinoWriteThread.writeThread().run(self.bolt,sharedArray1,s2)
sT2 = arduinoReadThread.readThread().run(self.bolt,sharedArray1,s1)
sT3 = arduinoReadThread.readThread().run(self.bolt,sharedArray2,s2)
sT4 = arduinoWriteThread.writeThread().run(self.bolt,sharedArray2,s1)
sT1.start()
sT2.start()
sT3.start()
sT4.start()
x = ControllerClass()
x

两个线程:写主题:

导入线程 类 writeThread (threading.Thread):

    def run ( self,bolt,writeList,sockeToWriteTo ):
s = sockeToWriteTo
while(bolt == 0):
conn, addr = s.accept()
if len(writeList) > 0:

socket.send(writeList.pop(0))

阅读话题

import threading
class readThread ( threading.Thread ):

def run ( self,bolt,writeList,socketToReadFrom ):
s = socketToReadFrom
while(bolt == 0):
conn, addr = s.accept()

f = conn.rcv()
print f
writeList.append(f)

最佳答案

你真的不需要线程......

接受新连接后,将其添加到列表中。当从列表中的连接之一接收到任何内容时,发送到除您从中收到消息的连接之外的所有连接。

使用select查看哪些连接已向您发送数据。

编辑

使用选择的例子:

# serversocket: One server socket listening on some port, has to be non-blocking
# all_sockets : List containing all connected client sockets
while True:
readset = [serversocket]
readset += all_sockets

# Wait for sockets to be ready, with a 0.1 second timeout
read_ready = select.select(readset, None, None, 0.1)

# If the listening socket can be read, it means it has a new connection
if serversocket in read_ready:
new_connection = serversocket.accept()
new_connection.setblocking(0); # Make socket non-blocking
all_sockets += [new_connection]
read_ready.remove(serversocket) # To not loop over it below

for socket in read_ready:
# Read data from socket
data = socket.recv(2048)
for s in all_sockets:
# Do not send to self
if s != socket:
s.send(data)

免责声明 我从来没有真正使用过 Python socket 函数,上面的代码是根据刚才阅读手册页编写的。代码可能不是最优的或者非常 Pythonic。

关于python - 从一个 socket 传递到另一个 socket ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8256085/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com