使用 TCP 的心跳原型的良好超时 - Java Sockets
我用 Java 实现了我自己的原型,它将使用 TCP 连接上的心跳,我也用它来传输消息和文件。 对于客户端,我使用阻塞套接字 I/O。 所以这就是我计划如何运作...... 我将socket超时设置为K秒,并使心跳保持在时间间隔T内发送消息,使得T < K. 如果超时到期,则看起来服务器对于客户端来说已离线,反之亦然。
心跳是服务器发送一个字符串,客户端回复另一个字符串。
我不想浪费大量带宽,但如果超时很大,服务器可能会错误地判断客户端的状态。
什么是好的K区间?我正在考虑 40 秒
PS:字符串是 ISO-8859-1 中发送的 8 个字母,因此数据很小
Im implementing my own proto in Java, it will use a heartbeat over a TCP connection that I also use to transfer messages and files.
For the client I am using blocking socket I/O.
So here is how Im planning that will work...
I will set the socket timeout for K seconds and make the heartbeat stay sending messages in an interval T, so that T < K.
If the timeout expires, looks like the server is offline for the client, or vice-versa.
The heartbeat is the server sending a String and the Client answering another.
I dont want to waste a lot of bandwidth but with a big timeout the server could be mistaken about the client´s status.
What is a good K interval? I am thinking about 40 seconds
PS: the strings are 8 letters sent in ISO-8859-1 so its small data
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
“K”高度依赖于应用程序的特定流量配置文件(例如,应用程序数据传输的频率)以及应用程序对连接丢失的容忍度(例如,接收器需要多快地检测到连接丢失)。不幸的是,低开销和快速检测是相反的目标。
听起来您试图解决 TCP 半开连接问题(RFC 793 含义) 。如果是这样,并且您不知道“半开放”一词,那么研究 TCP“半开放”可能会为您提供一些有用的信息,例如:
http://www.codeproject.com/Articles/37490/Detection-of-Half-Open -Dropped-TCP-IP-Socket-Conne.aspx
"K" is highly dependent on the specific traffic profile of the application (e.g. how often is application data transfered) and the application's tolerance for loss of connection (e.g. how quickly does the receiver need to detect the loss of connection). Unfortunately, low overhead and quick detection are opposing goals.
It sounds like your trying to address the TCP half-open connection issue (RFC 793 meaning). If so and if you weren't aware of the term "half-open", researching TCP "half-open" might point you toward some useful information like:
http://www.codeproject.com/Articles/37490/Detection-of-Half-Open-Dropped-TCP-IP-Socket-Conne.aspx
我认为这里没有通用的“正确”答案,这实际上取决于您的需求。您需要平衡带宽要求和服务认为客户端仍然存在的成本。这些需求因应用而异。对于 IM,您可能想在几秒钟内知道,但对于文件传输应用程序,几分钟可能是可以接受的。
I don't think there's a generic "right" answer here, it's really depending on your needs. You need to balance the bandwidth requirements and the cost of having the service thinking a client is still there. Those needs are different depending on the application. For IM, you probably want to know within a few seconds, but for a file transfer app, a few minutes might be acceptable.