TCP/IP Protocol learning Seven (TCP keepalive timer keepalive option)

Source: Internet
Author: User
Tags reset terminates time interval

Consider this scenario, a TCP connection has been established, but there is no data interaction between the two sides, so that regardless of the two host router between the crash, or the network cable is broken, as long as two host is not restarted, the connection remains. In this way, unnecessary bandwidth is wasted and costs increase. In fact, there is a keepalive timer in the TCP protocol.

If a connection does not take any action for a long time, the server sends a probe segment to the client. The client then has the following four kinds of feedback:

1, the client returns the normal ACK, the server knows that the client host is alive, the KeepAlive timer resets after two hours. If there is data interaction on this connection before the two-hour time expires, the keepalive timer is reset for the next 2 hours;

2. The client host has crashed and is shutting down or restarting. The server cannot receive a profiling response, and the default time-out after 75 seconds. The server sends a total of 10 probes, every 75 seconds, and terminates the connection if a response is not received;

3. The client host crashes and has restarted. The server receives a sniffing response, this response is only a reset, and the server terminates the connection after receiving it;

4, the client host normal operation, but the server is not reachable, this situation is similar to 2.

The Linux keepalive option is off by default, and opening the keepalive option when writing the socket program is as follows:

int keepAlive = 1; 	 Turn on the KeepAlive property. Default value: 0 (off)
int keepidle =;	 If there is no data interaction within 60 seconds, the probe is made. Default value: 7200 s
int keepinterval = 5;	The detection packet is detected at a time interval of 5 seconds. Default value: Keepcount s
int = 2;	 Number of probe retries. All timeouts determine that the connection is invalid. Default value: 10 times
setsockopt (S, Sol_socket, So_keepalive, (void*) &keepalive, sizeof (KEEPALIVE));
SetSockOpt (S, Sol_tcp, Tcp_keepidle, (void*) &keepidle, sizeof (Keepidle));
SetSockOpt (S, Sol_tcp, TCP_KEEPINTVL, (void*) &keepinterval, sizeof (Keepinterval));
SetSockOpt (S, Sol_tcp, tcp_keepcnt, (void*) &keepcount, sizeof (Keepcount));
The location in the Linux system configuration is as follows:

/proc/sys/net/ipv4/tcp_keepalive_time 
 /PROC/SYS/NET/IPV4/TCP_KEEPALIVE_INTVL 
 



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.