TCP keepalive how does it work

tcp

I run on Ubuntu and i want to use TCP keepalive in order to implement hearbeat between master-slave.both servers are my impl (c++).
I read this overview and did some google.
i didnt understand what TCP keepalive send and i do know if connection is no longer valid when use in TCP keepalive ?. Do i need to check the revived content?

The target is to have hearbeat between master-slave , so if one failed the other will be the master.
in this post the advice is to implement hearbeat with tcp keepalive mechanism, so this is the reason that i am asking how i identify the broken connection when using tcp keepalive.

Best Answer

You seem to be confusing the TCP keepalive mechanism with a load balancing daemon called keepalived. The two have nothing to do with each other. And if you're trying to detect connection loss or problems between two programs, both of which you've written, you don't want either mechanism.

If you want to detect a bad connection between two programs when you've written both of them, just build dead connection detection into the code. TCP guarantees that a side that is sending will detect a dead connection. So the simplest solution is to ensure that both sides send periodically. Another solution is to ensure one side sends periodically and the other side declares the connection dead if it does not receive that periodic data.