Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Multithreading 关闭后将客户端重新连接到服务器_Multithreading_Sockets_Python 2.7 - Fatal编程技术网

Multithreading 关闭后将客户端重新连接到服务器

Multithreading 关闭后将客户端重新连接到服务器,multithreading,sockets,python-2.7,Multithreading,Sockets,Python 2.7,我使用的代码开关只允许4个不同端口上的4个连接。 此代码正在工作,但当客户端关闭连接时,它无法重新建立连接。连接被拒绝。我想这是因为线程是闭合的。如何解决这个问题? 我无法更改端口号 enter code here --编码:cp1252-- 从套接字导入* BUFF=25 def服务器(主机、端口): 如果name='main': 导入线程 HOST = '192.168.0.12' PORTS = [10001,10002,10003,10004] threads = [] for por

我使用的代码开关只允许4个不同端口上的4个连接。 此代码正在工作,但当客户端关闭连接时,它无法重新建立连接。连接被拒绝。我想这是因为线程是闭合的。如何解决这个问题? 我无法更改端口号

enter code here
--编码:cp1252-- 从套接字导入*

BUFF=25

def服务器(主机、端口):

如果name='main': 导入线程

HOST = '192.168.0.12'
PORTS = [10001,10002,10003,10004]
threads = []
for port in PORTS:
    th = threading.Thread(target=server, args=(HOST, port))
    th.start()
    threads.append(th)

我不熟悉Python,但我会尝试一下

在线程中,您可能需要添加一个循环来接受新的连接。accept()和调用“handler”将在该循环中。这应该允许客户端在关闭连接时再次连接到服务器

以下是我认为它应该是什么样子(不包括错误检查,也不了解Python):

HOST = '192.168.0.12'
PORTS = [10001,10002,10003,10004]
threads = []
for port in PORTS:
    th = threading.Thread(target=server, args=(HOST, port))
    th.start()
    threads.append(th)
while  1: 
     clientsock, addr = serversock.accept()
     handler(clientsock,addr);