0
我正在使用充當服務器的程序,因爲它接收TCP/IP連接而不是進行連接。當程序開始時,我能打開程序中的TCP/IP端口,並使用連接到它:Python更新同一端口上的新客戶端的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
如果我打開一個新的端口,比如20101,在節目中,並嘗試重新連接到它,我得到:
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
我只能夠當我重新啓動該程序,重新打開端口成功重新連接,並重新啓動我的Python內核。
我想在程序關閉並重新打開後仍然可以與程序通信,而無需重新啓動我的Python內核。是否有可能這樣做?如果不是,爲什麼?如果是這樣,我如何增加我的代碼來實現這一目標?
您的服務器能夠一次接受多個連接嗎?客戶端斷開連接時是否正確關閉連接?顯示您的服務器代碼或顯示問題的[MCVE](http://stackoverflow.com/help/mcve)。 – mhawke