Several additional options for TCP keepalive

Source: Internet
Author: User

The tcp_keepalive option is just a switch, and the default keepalive option in Linux is as follows:

$sudo Sysctl-a | grep keepalivenet.ipv4.tcp_keepalive_time = 7200net.ipv4.tcp_keepalive_probes = 9NET.IPV4.TCP_KEEPALIVE_INTVL = 75

The KeepAlive option above indicates that if there is no data sent after 7200s on a connection, this option is set to send the KeepAlive keepalive message to the peer, which will have the following three results:

    • Reply ACK to the end. The local TCP considers the connection to remain alive. Continue to wait 7200s before sending the keepalive message.
    • Reply-to-end reset. Indicates that the peer-to-peer process has been restarted and the application on this side should close the connection.
    • There is no reply to the end. The local side does the retry, if the Retry 9 times (75 seconds before and after the retry interval is still unreachable), then returns an error message to the application, etimeout (no answer) or ehost

What if the application changes the default behavior of KeepAlive? The answer is to take advantage of the TCP options Tcp_keepidle, TCP_KEEPINTVL, tcp_keepcnt, and first look at how to use:

int setkeepalive (int fd, int interval) {int val = 1; if (setsockopt (FD, Sol_socket, so_keepalive, &val, sizeof (val)) = = = 1) {printf ("setsockopt so_keepalive:%s",        Strerror (errno));    return-1; }/* Send first probe after ' interval ' seconds.    */val = interval; if (setsockopt (FD, IPPROTO_TCP, Tcp_keepidle, &val, sizeof (val)) < 0) {printf ("setsockopt tcp_keepidle:%s\        N ", Strerror (errno));    return-1; }/* Send next probes after the specified interval. Note that we set the * delay as INTERVAL/3, as we send three probes before detecting * A error (see the next SE Tsockopt call).    */val = INTERVAL/3;    if (val = = 0) val = 1; if (setsockopt (FD, IPPROTO_TCP, TCP_KEEPINTVL, &val, sizeof (val)) < 0) {printf ("setsockopt TCP_KEEPINTVL:%        S\n ", Strerror (errno));    return-1; }/* 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) {printf ("SetSockOpt tcp_keepcnt:%s        \ n ", Strerror (errno));    return-1; } return 0;}
    • Tcp_keepdile set Connection If no data sent, how long after sending KeepAlive detection packet, unit is seconds
    • Time interval between two probes before and after TCP_KEEPINTVL, unit is seconds
    • tcp_keepcnt Maximum number of retries before closing a non-active connection

With tcpdump, you can see the detailed behavior of the end that sets the option above.

date:2015-07-17t19:45+0800

Author:cobbliu

ORG version 7.9.3f with Emacs version 24

Several additional options for TCP keepalive

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.