Good timeout for heartbeating proto using TCP - Java Sockets

两盒软妹~` 提交于 2019-12-22 01:15:54

问题


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


回答1:


"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




回答2:


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.



来源:https://stackoverflow.com/questions/3781045/good-timeout-for-heartbeating-proto-using-tcp-java-sockets

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!