连接失败后重新建立Python Socket

时间:2019-07-25 19:09:15

标签: python sockets tcp

我正在使用python套接字通过无线IP连接控制某些内容。在管道故障后自动重新建立连接非常重要,这通常在设备碰到无线死区时发生。

首先,我使用两端的python程序在客户端和服务器之间建立简单的套接字连接。为了验证是否存在连接,我从客户端向服务器发送了一条简短消息。

如果套接字握手失败,我会抓住它:

try:
    s.send(MESSAGE)
except socket.error, e:
     s.connect((IP,PORT))
     #In reality, the s.connect wouldn't be right here
     #I would use Loops to continuously re-establish on a socket error.
     #I would also use the try command for s.connect.  

但是,我无法使用此方法重新连接,因为

socket.error: [Errno 106] Transport endpoint is already connected

在此错误中,有时我还会遇到错误98,该地址已被使用。

是否有任何方法可以解决这些错误,或者建议使用其他协议以在较差的网络连接上具有更好的“自我修复”特性?

0 个答案:

没有答案