Basic introduction
Keep alive can be set at the operating system level for connections made to this machine. A simple TCP packet is returned to the remote host within the set time to detect if the remote host is still responding.
There are 2 main application scenarios:
1. Know earlier that the remote host was down.
Under normal circumstances a host and a Host B have established a connection. A sent a message to the B host, B said received, a began to wait. But B host down, but B did not say to a that they have been down, so a will always wait (unless the application is set to have a timeout mechanism). Using the keep alive can detect B down earlier.
2. The other network devices between a and B actively shut off the connection that is too idle, because the network device used to keep 2 machines connected requires resources and resources are limited.
If a, the application on B sometimes takes longer to exchange requests, such as the need to create a large index. After a period of time a through the established connection to the B communication, but this connection has been in the middle of some devices have been shut down. Will cause unnecessary trouble. So you can pass > keep alive mechanism, periodically send some requests, so that those intermediate devices know that there are 2 of machines there is interaction, so that the connection will not be closed.
To solve the above 2 problems, most programs do not implement a keep alive function on their own. When it is convenient to use this service provided by Linux, just when creating the socket, tell Linux Kernel that I need your Keep alive function.
int 1 sizeof(keepalive));
Related parameters:
Tcp_keepalive_time
The time interval for sending keep alive packets, in seconds.
Tcp_keepalive_intvl
If sent, but no response. It will be a long time before we send another one.
Tcp_keepalive_probes
Send the number of no response, will think the other side is down.
# cat/proc/sys/net/ipv4//proc/sys/net/ipv4//proc/sys/net/ipv4/' ' >/proc/sys/net/ipv4/ >/proc/sys/net/ipv4/ >/proc/sys/ Net/ipv4/tcp_keepalive_probes
The above method is only valid for the current Linux instance. If you want to ensure that you always need to modify/etc/sysctl.conf
Net.ipv4.tcp_keepalive_time=60
Net.ipv4.tcp_keepalive_intvl=60
Net.ipv4.tcp_keepalive_probes=20
Reference documents
Http://tldp.org/HOWTO/TCP-Keepalive-HOWTO/overview.html
Http://tldp.org/HOWTO/TCP-Keepalive-HOWTO/usingkeepalive.html
http://stackoverflow.com/questions/17740492/ How-i-will-use-setsockopt-and-getsockopt-with-keep-alive-in-linux-c-programming
Linux Keep alive