Linux So_keepalive Properties, Heartbeat

Source: Internet
Author: User
Tags ack keep alive

For a connection-oriented TCP socket, in practice, it is common to check whether the peer is in the connection, the connection port 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, for example, the network is broken, suddenly power outages.
For another scenario, there are several ways to infer whether a connection is broken:
1, to write their own heartbeat package program, simply said that its own program to add a thread, timed to send packets to the peer, to see if there is an ACK, according to 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, to the server as an example, when the servers detected to more than a certain time (/proc/sys/net/ipv4/tcp_keepalive_time 7200, 2 hours) did not transfer data, 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 to a Ack.server end received an ACK after resetting the timer, after 2 hours in the transmission of the probe. Assuming that the data is transmitted over the 2-hour connection, the delay is deferred 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 previously crashed, but has restarted. The probe response received by the server is a reset, and the server end terminates the connection.

system default value for changing three parameters
Temporary method: to three files directly write the parameters, the system restart needs to be set again;
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;}


Linux So_keepalive Properties, Heartbeat

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.