我正在使用 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。
是否有任何方法可以解决这些错误,或者推荐一种在网络连接不佳时具有更好“自我修复”特性的不同协议?