检测损坏的套接字 [英] Detecting a broken socket
问题描述
我正在使用TCP IP套接字。
I'm working with TCP IP Socket.
这个是检测客户端套接字连接断开的最简单方法吗?
Is THIS the simplest way to detect that a client socket connection is broken?
如果没有检测到死客户端的Java API,我会感到惊讶并抛出异常。至少当服务器尝试从缺少ACK响应向客户端发送数据包时,这应该是可能的。
I would be surprised if there is no Java API that detects a dead client and throws an exception. This should be possible at least when the server tries to send a packet to the client from the lack of an ACK response.
推荐答案
TCP协议不支持任何检测断开连接的快速方法。
TCP protocol does not support any fast means of detecting a broken connection.
这个问题(及其所有答案)全面解释了这个问题: TCP套接字连接是否具有保持活动状态?
This question (and all of it's answers) explain the problem in full: Does a TCP socket connection have a "keep alive"?
所以基本上是的,这是最简单的方法。您需要在应用程序协议中实现乒乓包(也称为HearBeat包)以正确验证连接断开。
So basically yes, that is the simplest way. You need to implement ping-pong packets (aka HearBeat packet) in your application protocol to verify the broken connection properly.
这篇关于检测损坏的套接字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!