The common task for any TCP networking application is to observe connection problems.
Some devepers prefer to implement their own application level “heartbeat” protocol which makes sure that TCP connection is alive.
Not all know that TCP implementations indeed have built-in way to detect and signalize connection failure.
The built-in way is called TCP “keep alives” mechanism for detecting connections problems.
It is not turned on by default since it generates additional packets but it is DEFINITELY more robust than most home made solutions.
You can read more here:
TCP keep alives on Windows
TCP keep alives on Linux
Okay, this is awesome. I had no idea it existed. I’ve never had a need to prevent disconnection due to inactivity, but as you know, I am always on the lookout for more robust ways of detecting when my peer has died without properly closing its TCP connection.
–Dave
Keepalives by default are sent very slowly. Most application-level heartbeats operate at much higher frequencies, may contain additional information, *and* may be usable with other protocols, such as UDP (connectionless and therefore impossible to “keep-alive”). Ultimately, there is no difference in robustness in either case, because you can achieve the exact same behavior in terms of messages sent. It’s just a tradeoff between simplicity (tcp keepalive) and power (application heartbeat)