Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/364.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/5/tfs/3.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
Python更新同一端口上新客户端的TCP/IP套接字_Python_Sockets_Tcp Ip - Fatal编程技术网

Python更新同一端口上新客户端的TCP/IP套接字

Python更新同一端口上新客户端的TCP/IP套接字,python,sockets,tcp-ip,Python,Sockets,Tcp Ip,我正在使用一个充当服务器的程序,因为它接收TCP/IP连接,而不是建立连接。当程序启动时,我能够在程序内打开TCP/IP端口,并使用以下方式连接到该端口: import socket s = socket.create_connection(("localhost",20100)) s.send("PAUSE\30") s.recv(1024) 这将暂停我的程序。我可以继续以这种方式发送命令。但是,如果我关闭程序,重新打开程序,重新打开程序中的端口,然后尝试相同的命令,我会收到: s.send

我正在使用一个充当服务器的程序,因为它接收TCP/IP连接,而不是建立连接。当程序启动时,我能够在程序内打开TCP/IP端口,并使用以下方式连接到该端口:

import socket
s = socket.create_connection(("localhost",20100))
s.send("PAUSE\30")
s.recv(1024)
这将暂停我的程序。我可以继续以这种方式发送命令。但是,如果我关闭程序,重新打开程序,重新打开程序中的端口,然后尝试相同的命令,我会收到:

s.send("PAUSE\30")
s.recv(1024)

---------------------------------------------------------------------------
error                                     Traceback (most recent call last)
<ipython-input-7-40f527ce990f> in <module>()
      1 s.send("PAUSE\30")
----> 2 s.recv(1024)[1:-1]

error: [Errno 10053] An established connection was aborted by the software in your host machine
s = socket.create_connection(("localhost",20101))

---------------------------------------------------------------------------
error                                     Traceback (most recent call last)
<ipython-input-8-857374ef86d3> in <module>()
----> 1 s = socket.create_connection(("localhost",20101))

C:\Program Files\Enthought\Canopy\App\appdata\canopy-1.5.5.3123.win-x86_64\lib\socket.pyc in create_connection(address, timeout, source_address)
    569 
    570     if err is not None:
--> 571         raise err
    572     else:
    573         raise error("getaddrinfo returns an empty list")

error: [Errno 10061] No connection could be made because the target machine actively refused it
s.send(“暂停\30”)
s、 recv(1024)
---------------------------------------------------------------------------
错误回溯(最近一次呼叫上次)
在()
1 s.发送(“暂停\30”)
---->2 s.recv(1024)[1:-1]
错误:[Errno 10053]主机中的软件中止了已建立的连接
如果我在程序中打开一个新端口,例如20101,并尝试再次连接到该端口,我将收到:

s.send("PAUSE\30")
s.recv(1024)

---------------------------------------------------------------------------
error                                     Traceback (most recent call last)
<ipython-input-7-40f527ce990f> in <module>()
      1 s.send("PAUSE\30")
----> 2 s.recv(1024)[1:-1]

error: [Errno 10053] An established connection was aborted by the software in your host machine
s = socket.create_connection(("localhost",20101))

---------------------------------------------------------------------------
error                                     Traceback (most recent call last)
<ipython-input-8-857374ef86d3> in <module>()
----> 1 s = socket.create_connection(("localhost",20101))

C:\Program Files\Enthought\Canopy\App\appdata\canopy-1.5.5.3123.win-x86_64\lib\socket.pyc in create_connection(address, timeout, source_address)
    569 
    570     if err is not None:
--> 571         raise err
    572     else:
    573         raise error("getaddrinfo returns an empty list")

error: [Errno 10061] No connection could be made because the target machine actively refused it
s=socket.create_连接((“localhost”,20101))
---------------------------------------------------------------------------
错误回溯(最近一次呼叫上次)
在()
---->1s=socket.create_连接((“localhost”,20101))
C:\Program Files\Enthught\Canopy\App\appdata\Canopy-1.5.5.3123.win-x86\u 64\lib\socket.pyc在创建\u连接中(地址、超时、源\u地址)
569
570如果err不是无:
-->571上升误差
572其他:
573 raise错误(“getaddrinfo返回空列表”)
错误:[Errno 10061]无法建立连接,因为目标计算机主动拒绝了它
只有重新启动程序、重新打开端口并重新启动Python内核,我才能成功地重新连接


我希望在程序关闭并重新打开后仍能与它通信,而无需重新启动Python内核。有可能吗?若否,原因为何?如果是这样的话,我该如何扩充代码来实现这一点呢?

我看到的问题是缺少s.close()。您需要在关闭或退出程序时正确关闭连接,才能使实际的套接字描述符空闲。您正在呼叫吗?

您的服务器一次可以接受多个连接吗?客户端断开连接时是否正确关闭连接?显示服务器代码,或演示问题的。