私は単純なマルチスレッドTCPサーバーを実装しようとしています。接続されたクライアントが1つだけの場合はうまく動作しますが、同時に2つのクライアントが接続されている場合、第1のクライアントのスレッドは、第2のクライアントが受け取るメッセージを受信することがあります。彼の問題にどう対処するのですか?Python。マルチスレッドソケットtcpサーバー
class ClientThread(Thread):
def __init__(self, ip, port):
Thread.__init__(self)
self.ip = ip
self.port = port
#...
def run(self):
while True:
try:
data = conn.recv(1024)
#...
except ConnectionResetError:
break
TCP_IP = '0.0.0.0'
TCP_PORT = 1234
BUFFER_SIZE = 1024
tcpServer = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
tcpServer.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
tcpServer.bind((TCP_IP, TCP_PORT))
threads = []
while True:
tcpServer.listen(4)
(conn, (ip, port)) = tcpServer.accept()
newthread = ClientThread(ip, port)
newthread.start()
threads.append(newthread)
for t in threads:
t.join()
は、右のクライアントに適切なメッセージを送信し、また、(4) 'ループのうち' tcpServer.listenを取るために、 'conn'ソケットを使用します –