twisted未能检测到客户端断开连接
有没有人有类似的经验?我有一个使用Twisted框架的应用程序。客户端会连接到服务器。我添加了一个功能,如果一个客户端连接到服务器,但这个IP地址已经有一个客户端在运行,那么新连接的客户端会被断开。
有时候,我会直接关掉一个客户端电脑(或者说是虚拟机),而没有手动关闭Python程序。每当我这样做的时候,偶尔会出现服务器没有检测到断开的情况。当电脑重新启动并尝试重新连接时,服务器却坚持认为这个IP地址已经有连接了。到目前为止,我找到的唯一解决办法就是重启服务器。
这可能是网络方面的奇怪问题导致断开信号没有传递过去吗?或者是Twisted框架的bug?
我几乎可以肯定这不是我处理断开的代码的问题。我的代码设置是这样的:只要连接断开,就会调用connectionLost
,这包括大多数情况下关闭机器的情况。如果断开是因为某种奇怪的情况,它会记录一条信息或者抛出一个异常。但这些情况在日志中都没有出现。
2 个回答
2
哈哈,我真不敢相信我把在网络课上学的所有东西都忘了...
(2:09:44 PM) coworker: this is the expected behaviour
(2:10:15 PM) coworker: the server has no way to know if someone dies, or is just quiet
(2:10:35 PM) coworker: unless ofcourse the server has some kind of ping/keepalive message
(2:15:38 PM) claudiu: ah so if they have no communicatin
(2:15:42 PM) claudiu: there's no way to tell that a TCP connection has died
(2:15:47 PM) claudiu: i remember learning that now, yes..
(2:16:23 PM) claudiu: but if i just make the server ping the client then it'll figure out soon enough from lack of ACKs that it's dead, right?
(2:16:45 PM) coworker: right
5
这是一个Twisted常见问题解答,虽然它其实和Twisted没有特别直接的关系。