What is TCP Keep Alive?
This page answers questions like these:
- What is TCP KeepAlive?
- What is the use of TCP KeepAlive?
- What is the purpose of TCP KeepAlive?
- What problems can TCP KeepAlive solve?
- How do I stop a router dropping an idle TCP connection?
- How do I detect whether the other side of a TCP connection is alive or dead?
What Is TCP KeepAlive?
KeepAlive is a built-in function of TCP which, if enabled for a socket, will:
- Send “empty” packets across the connection whenever no data has been sent or received for a specified period of time.
- Declare that a socket connection is dead (and thus disconnect the socket) if the other side does not respond to the “empty” packets within a specified period of time.
Enabling KeepAlive for one end of a socket, will automatically send an “empty” packet across the socket (from the enabled end to the other end) whenever the enabled end detects that the socket has been idle for a given period of time (where “idle” means “no data has been sent or received”). Such a packet is often referred to as a probe.
An “empty” packet is in fact an ACK of an old packet number.
Upon receiving such an ACK, the receiving side will send an ACK with the current (expected) packet number.
These ACK’s do not affect, and are not visible to, the end user of the socket. They can only be seen using a packet sniffer.
What Problems Can TCP KeepAlive Solve?
Enabling KeepAlive on a socket can help you to do either of these two very different things:
Examples of these are, respectively:
- Keep an idle socket connection alive.
- Detect whether the computer at the other end of a socket is alive.
- Stop a router from dropping the socket connection because the router thinks the connection has been idle for too long.
- Detect whether the machine on the other end of a socket is still alive even though it hasn’t sent you any data for a long period of time.
How to Use TCP Keep Alive?
Home > C++ / C > What is TCP Keep Alive?
Tags: KeepAlive, keep alive, Keep an idle socket connection alive, Detect whether the computer at the other end of a socket is alive, setsockopt, socket, TCP, Transmission Control Protocol, C++, C