简体   繁体   English

使用TCP - Java套接字的心跳原型的良好超时

[英]Good timeout for heartbeating proto using TCP - Java Sockets

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. 我在Java中实现我自己的proto,它将使用TCP连接的心跳,我也用它来传输消息和文件。 For the client I am using blocking socket I/O. 对于客户端,我使用阻塞套接字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. 所以这就是我计划如何工作......我将设置套接字超时为K秒并使心跳保持在间隔T内发送消息,以便T <K。如果超时到期,看起来服务器处于脱机状态对于客户,反之亦然。

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? 什么是良好的K区间? I am thinking about 40 seconds 我在想40秒

PS: the strings are 8 letters sent in ISO-8859-1 so its small data PS:字符串是ISO-8859-1中发送的8个字母,因此它的数据很小

"K" is highly dependent on the specific traffic profile of the application (eg how often is application data transfered) and the application's tolerance for loss of connection (eg how quickly does the receiver need to detect the loss of connection). “K”高度依赖于应用程序的特定流量配置文件(例如,应用程序数据传输的频率)和应用程序对连接丢失的容忍度(例如,接收器检测连接丢失的速度有多快)。 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) . 这听起来像你试图解决TCP半开连接问题(RFC 793意义) 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: 如果是这样,如果你不知道“半开放”一词,研究TCP“半开放”可能会指向一些有用的信息,如:

http://www.codeproject.com/Articles/37490/Detection-of-Half-Open-Dropped-TCP-IP-Socket-Conne.aspx http://www.codeproject.com/Articles/37490/Detection-of-Half-Open-Dropped-TCP-IP-Socket-Conne.aspx

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. 对于IM,您可能想在几秒钟内知道,但对于文件传输应用程序,几分钟可能是可以接受的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM