UNIX network programming-socket options (Heartbeat detection, bind address multiplexing)

Source: Internet
Author: User
/* Set the socket option to periodically detect the connectivity heartbeat packet and Xinbo. It is mainly used for persistent connections. * Parameter: socket, 1 or 0 enabled, first interval, two intervals, number of disconnections */void setkeepalive (INT isockfd, int isockattron, socklen_t iidletime, socklen_t iinterval, socklen_t icount) {setsockopt (isockfd, sol_socket, so_keepalive, (const char *) & percent, sizeof (isockattron); setsockopt (isockfd, sol_tcp, cosine, (const char *) & iidletime, sizeof (iidletime); setsockopt (isockfd, sol_tcp, substring, (const char *) & iinterval, sizeof (iinterval); setsockopt (isockfd, sol_tcp, timeout, (const char *) & icount, sizeof (icount);}/* bind address reuse. it is mainly used on TCP server */setsockopt (isockfd, sol_socket, so_reuseaddr, & isockattron, sizeof (isockattron ));


So_keepalive keeps the connection to check whether the host of the other party crashes, so as to avoid the (server) from blocking the input of the TCP connection forever. After this option is set, ifThere is no data exchange in any direction (that is, the server does not send or receive data)TCP will automatically send a keepalive probe to the other party ). This is a TCP shard that the other party must respond to. It may cause the following three situations:

 

  • The other party receives the desired ack response.
  • 2 hours later, TCP sends out another detection shard. The other party has crashed and restarted: respond with RST. The pending error of the Set interface is setEconnreset(Recv returns 0, errno is econnreset), and socket itself is disabled.
  • The other party has no response: TCP sent from the Berkeley sends an additional eight detection segments, one in 75 seconds, and tries to get a response. If no response is returned after the first probe is sent for 11 minutes and 15 seconds (9*75 seconds. The pending error of the Set interface is setEtimeoutAnd the set interface itself is disabled. For example, if the ICMP error is "Host Unreachable (host inaccessible)", it indicates that the host of the other party has not crashed but is not reachable. In this case, the error to be handled is set to ehostunreach.

 

 

If we cannot accept such a long wait timeYou can know from TCP-keepalive-howto that there are two ways to set.

 

  • Modify kernel network configuration parameters:
echo 30 > /proc/sys/net/ipv4/tcp_keepalive_timeecho 10 > /proc/sys/net/ipv4/tcp_keepalive_intvlecho 3 > /proc/sys/net/ipv4/tcp_keepalive_probes

 

 

The tcp_keepidle, tcp_keepintvl, and tcp_keepcnt of the sol_tcp field.Three options:

 

1) The tcp_keepidle parameter specifies the interval of inactivity that causes TCP to generate a keepalive transmission for an application that requests them. tcp_keepidle defaults to 14400 (two hours ).

/* Tcp null close time before the first keepalive test */

 

2) The tcp_keepintvl parameter specifies the interval between the nine retries that are attempted if a keepalive transmission is not acknowledged. tcp_keepintvl defaults to 150 (75 seconds ).
/* Interval between two keepalive probes */


3) The tcp_keepcnt option specifies the maximum number of keepalive probes to be sent. The value of parameter is an integer value between 1 and N, where N is the value of the systemwide tcp_keepcnt parameter.

/* Determine the number of keepalive probes before disconnection

 

int                 keepIdle = 1000;int                 keepInterval = 10;int                 keepCount = 10;Setsockopt(listenfd, SOL_TCP, TCP_KEEPIDLE, (void *)&keepIdle, sizeof(keepIdle));Setsockopt(listenfd, SOL_TCP,TCP_KEEPINTVL, (void *)&keepInterval, sizeof(keepInterval));Setsockopt(listenfd,SOL_TCP, TCP_KEEPCNT, (void *)&keepCount, sizeof(keepCount));

 

Remember that keepalive is not program? Related, but socket? Related, so if you have multiple sockets, you can handle keepalive for each of them separately.

 

 

We need to pay attention to the TCP-keepalive-howto section:

Remember that keepalive is not program −related, but socket −related, so if you have multiple sockets, you can handle keepalive for each of them separately.

These attributes are inherited by sockt.That is, after the listen socket sets this attribute, the accept socket after the connection is established also inherits this attribute (Heartbeat attribute ).

If the heartbeats function maintains the survival of the client, that is, the server must send a certain amount of data to the client segment at intervals, so_keepalive is insufficient. Because the so_keepalive option refers"There is no data exchange in any direction of this interface". In the Linux 2.6 series, the above understanding is that as long as the set interface that opens the so_keepalive option detectsData transmission or data receivingIt is considered as data exchange. That is, in the case of abnormal disconnection (the server does not receive the "fin" or "rst" packet), the upper-layer program can still send the packet to the buffer normally, and the client has been abnormally disconnected, as a result, TCP will automatically re-transmit packets. If the priority of the re-transmitted packet is higher than that of the keepalive packet, it means that keepalive cannot be sent out. At this time, we do not know that the connection has been interrupted due to an error, after a long retransmission failure, we can only know that the client exits unexpectedly after keepalive is sent.

Http://blog.csdn.net/ctthuangcheng/article/details/9450087

 

UNIX network programming-socket options (Heartbeat detection, bind address multiplexing)

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.