Linux So_keepalive Properties, Heartbeat

Source: Internet
Author: User
Tags ack keep alive

For connection-oriented TCP sockets, in practical applications, it is common to detect whether the peer is in a connection, and the connection port is in two cases:
1, the connection is closed normally, call Close () shutdown () connection graceful Close, send and recv immediately return error, select Return sock_err;
2, the connection to the end of the abnormal shutdown, such as network disconnection, sudden power loss.
For the second case, there are several ways to determine if a connection is broken:
1, write their own heartbeat package program, simply said that their own program to join a thread, timed to send packets to the peer, to see if there is an ACK, based on the return of the ACK to manage the connection. This method is more common, generally using the business layer heartbeat processing, flexible and controllable, but changed the existing protocol;
2, using TCP keepalive mechanism, UNIX network programming does not recommend the use of so_keepalive to do heartbeat detection (why??).
KeepAlive principle: TCP embedded with a heartbeat packet, with the server as an example, when the servers detected more than a certain time (/proc/sys/net/ipv4/tcp_keepalive_time 7200, 2 hours) no data transfer, Then a keepalive packet is sent to the client side, at which point there are three types of reactions:
1, the client connection is normal, return a Ack.server end received an ACK after resetting the timer, after 2 hours in the sending probe. If there is data transmission on the connection within 2 hours, then the detection packet is delayed for 2 hours on the basis of the time;
2, the client shuts down unexpectedly, or the network disconnects. The client is unresponsive, the server does not receive an ACK, and after a certain amount of time (/PROC/SYS/NET/IPV4/TCP_KEEPALIVE_INTVL 75 is 75 seconds) the KeepAlive packet is re-sent, and a certain number of times (/proc/ Sys/net/ipv4/tcp_keepalive_probes 9 i.e. 9 times);
3, the client has crashed, but has been restarted. The probe response received by the server is a reset, and the server side terminates the connection.

Modify the system default value of three parameters
Temporary method: Write the parameters directly to three files, the system restart needs to be reset;
Temporary method: Sysctl-w net.ipv4.tcp_keepalive_intvl=20
Global Settings: You can change the/etc/sysctl.conf, plus:
NET.IPV4.TCP_KEEPALIVE_INTVL = 20
Net.ipv4.tcp_keepalive_probes = 3
Net.ipv4.tcp_keepalive_time = 60

/* Set TCP keep alive option to detect dead peers. The interval option * is only used for Linux as we have using linux-specific APIs to set * The probe send time, interval, a nd count. */int anetkeepalive (char *err, int fd, int interval) {int val = 1;//open keepalive mechanism if (setsockopt (FD, Sol_socket, So_        KEEPALIVE, &val, sizeof (val)) = =-1) {Anetseterror (err, "setsockopt so_keepalive:%s", Strerror (errno));    return anet_err;  } #ifdef __linux__/* Default settings is more or less garbage, with the keepalive time * set to 7200 by Default on Linux. Modify settings to make the feature * actually useful. */* Send first probe after interval.    */val = interval; if (setsockopt (FD, IPPROTO_TCP, Tcp_keepidle, &val, sizeof (val)) < 0) {Anetseterror (err, "setsockopt Tcp_ke        Epidle:%s\n ", Strerror (errno));    return anet_err; }/* Send next probes after the specified interval. Note that we set the * delay as INTERVAL/3, as we Send three probes before detecting * An error (see the next setsockopt call).    */val = INTERVAL/3;    if (val = = 0) val = 1; if (setsockopt (FD, IPPROTO_TCP, TCP_KEEPINTVL, &val, sizeof (val)) < 0) {Anetseterror (err, "setsockopt tcp_k        EEPINTVL:%s\n ", Strerror (errno));    return anet_err; }/* Consider the socket in error state after three we send three ACK * probes without getting a reply.    */val = 3; if (setsockopt (FD, IPPROTO_TCP, tcp_keepcnt, &val, sizeof (val)) < 0) {Anetseterror (err, "setsockopt Tcp_kee        PCNT:%s\n ", Strerror (errno));    return anet_err; } #endif return ANET_OK;}


 

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.