TCP Keep-Alive Messages
(2010-05-19 09:31:23)From:MSDN
A Transmission Control Protocol (TCP) keep-alive packet is an acknowledgment (ACK) with the sequence number set to one less than the current sequence number for the connection. The Transmission Control Protocol/Internet Protocol (TCP/IP) stack can automatically generate these keep-alive messages to verify that the computer at the remote end of a connection is still available.
For example, a client application that downloads data from a server and then performs a long series of calculations might need to periodically verify that the server that will receive its results is still reachable. If the server is not reachable, and there is no alternative server to upload the results to, it might not be useful for the application to continue its calculations. Similarly, a server holding resources in a cache might need to periodically verify that its clients are still connected, so that it can deallocate cached resources used by disconnected clients.
When implementing an existing protocol, developers must follow the protocol's guidelines on use or nonuse of keep-alives. When designing a new protocol, developers specify their own keep-alive strategies. Some reasons that a developer might decide not to use TCP keep-alives include:
-
The application protocol includes its own higher-level keep-alive mechanism.
-
The application does not need to do anything, such as invalidate cache entries, if a connection has been idle for a long while.
-
The application runs on a busy network and TCP keep-alives would generate too much traffic.
A host receiving a keep-alive ACK responds with an ACK for the current TCP sequence number.
Two per-interface registry settings under the key
/HKEY_LOCAL_MACHINE/SYSTEM/CurrentControlSet/Tcpip/Parameters
control the behavior of TCP/IP keep-alives:
-
The KeepAliveTime value specifies how long the TCP connection sits idle, with no traffic, before TCP sends a keep-alive packet. The default is 7,200,000 milliseconds (ms) or 2 hours.
-
The KeepAliveInterval value indicates how many milliseconds to wait for a response after sending a keep-alive before repeating the keep-alive. If no response is received, the TCP/IP stack continues sending keep-alives at this interval until a response is received or until the stack reaches the packet retry limit specified in the TCPMaxDataRetransmissions registry key.KeepAliveInterval defaults to 1 second (1000 ms).
NetBIOS over TCP (NetBT) connections, such as those used by many Microsoft networking components, send NetBIOS keep-alives more frequently than the usual rate for TCP keep-alives. Thus, generally no TCP keep-alives are sent on a NetBIOS connection.
TCP keep-alives are disabled by default, but Windows Sockets applications can use the setsockoptfunction to enable them on a per-connection basis.
Note If the developer elects to use TCP keep-alive messages on a particular connection, the timing of those messages is specified by the registry values described preceding. It is not possible to use different timing on different keep-alive requests.