The heartbeat mechanism under the socket connection under Linux

Source: Internet
Author: User

1, under a long connection , there may be a long period of time without data exchange .

Theoretically, this connection is always connected, but in practice, it is difficult to know what is wrong with the intermediate node.

Some nodes (firewalls) automatically break down connections that have no data interaction for a certain amount of time.

At this time, we need our heartbeat package, to maintain a long connection, keepalive

2, the heartbeat packet is called the heartbeat packet is because: it is like a heartbeat, every fixed time to send to tell the server, the client is still alive. In fact, this is to maintain a long connection, as for the contents of this package, there is no special provisions, but generally is a small package, or only a packet of empty packets in Baotou. The heartbeat packet is mainly used for long-connection keepalive and disconnection processing. In general application, the judgment time in 30-40 seconds is quite good. If the demand is high, it will be 6-9 seconds.


3, the following is packaged in the heartbeat packet function, added to the project parameter settings can be

#include <netinet/tcp.h>//parameter explanation//FD: Network connection Descriptor//start: Idle time between the first heartbeat packet send//interval: Two heartbeat detection packet interval//count: Number of probes,       A few probing failures are determined to be TCP break int set_tcp_keepalive (int fd, int start, int interval, int count) {int keepAlive = 1;   if (FD < 0 | | | Start < 0 | | Interval < 0 | | Count < 0) return-1;    Entrance parameter check, good habit of programming. Enable heartbeat mechanism, if you want to close, place KEEPALIVE 0 if (setsockopt (fd,sol_socket,so_keepalive, (void*) &keepalive,sizeof (KEEPALIVE)           ) = =-1) {perror ("setsockopt");       return-1;       }//enable the heartbeat mechanism to start the idle time between the first heartbeat detection packet send if (setsockopt (Fd,sol_tcp,tcp_keepidle, (void *) &start,sizeof (start)) = =-1)           {perror ("setsockopt");       return-1;       }//Two heartbeat detection packets between the time if (setsockopt (FD,SOL_TCP,TCP_KEEPINTVL, (void *) &interval,sizeof (interval)) = =-1)           {perror ("setsockopt");       return-1; }//The number of probes that will be detected on several attempts to fail is TCP disconnect if (setsockopt (fd,sol_tcp,tcp_keepcnt, (void *) &count,sizeof (cOunt)) = =-1) {perror ("setsockopt");       return-1;   } return 0;  }

Pass the parameter that you want to set to the function, the setting returns 0 successfully, otherwise-1. After the setting is successful, the FD can be handed to select to listen for a readable writable event, if Select detects FD readable and read returns an error, it is generally possible to determine that the TCP connection corresponding to the FD has been abnormally fractured, call the close function to close the FD.


Detection of abnormal disconnection of TCP connections (KeepAlive probe)

The "abnormal disconnection" here means that the TCP connection is not broken gracefully, such as a physical link such as a network cable failure, and a sudden failure of the host to cause

There are two ways to detect: 1.TCP Connection timing Handshake message 2. Using keepalive detection in the TCP protocol stack

The second method is simple and reliable, requiring only two sockets for TCP connections to set the KeepAlive probe.

thus knowing that the connection is invalid, the client program has the opportunity to perform cleanup, alert the user, or reconnect .



The heartbeat mechanism under the socket connection under Linux

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.