TCP KeepAlive的几个附加选项

TCP_KEEPALIVE选项只是一个开关,Linux中默认的Keepalive的选项如下:

$sudo sysctl -a | grep keepalive
net.ipv4.tcp_keepalive_time = 7200
net.ipv4.tcp_keepalive_probes = 9
net.ipv4.tcp_keepalive_intvl = 75

上文中的keepalive选项表示如果一个连接上7200s后没有任何数据发送,则设置了这个选项的本端向对端发送keepalive保活报文,它会有如下三种结果:

  • 对端回复ACK。则本端TCP认为该连接依然存活。继续等7200s后再发送keepalive报文。
  • 对端回复RESET。说明对端进程已经重启,本端的应用程序应该关闭该连接。
  • 没有对端的任何回复。则本端做重试,如果重试9次(前后重试间隔为75秒)仍然不可达,则向应用程序返回错误信息,ETIMEOUT(无任何应答)或EHOST

如果应用程序向改变keepalive的默认行为,该怎么办呢?答案就是利用 TCP_KEEPIDLE、TCP_KEEPINTVL、TCP_KEEPCNT这几个TCP选项,首先看看如何使用:

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
     * 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) {
        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 设置连接上如果没有数据发送的话,多久后发送keepalive探测分组,单位是秒
  • TCP_KEEPINTVL 前后两次探测之间的时间间隔,单位是秒
  • TCP_KEEPCNT 关闭一个非活跃连接之前的最大重试次数

用tcpdump抓包就可以看到设置了上面选项的那端的详细行为。

Date: 2015-07-17T19:45+0800

Author: Cobbliu

Org version 7.9.3f with Emacs version 24

时间: 2024-10-25 21:09:24

TCP KeepAlive的几个附加选项的相关文章

tcp keepalive选项

之前一直对tcp keepalive选项理解有误, 以为通过setsockopt函数设置SO_KEEPALIVE和相关参数后该socket则使用设置的keepalive相关参数 否则使用系统默认的:keepalive配置(如下) [email protected]:/# sysctl -a | grep keepnet.ipv4.tcp_keepalive_intvl = 30net.ipv4.tcp_keepalive_probes = 9net.ipv4.tcp_keepalive_time

TCP KEEP-ALIVE和TCP_USER_TIMEOUT机制

正常通信的情况下,send函数发送成功会返回发送数据的字节数.当有错误发生时,send返回-1,全局变量errno被设置.很多情况下,send返回-1是由于连接被对端关闭(对端发送了RST或者FIN包),这种情况errno会被设置为ECONNRESET(Connection reset by peer). 可是在对端的网线被拔.网卡被卸载或者禁用的时候,对端没有机会向本地操作系统发送TCP RST或者FIN包来关闭连接.这时候操作系统不会认为对端已经挂了.所以在调用send函数的时候,返回的仍然

AIX/Linux/Solaris/HP-UXAIX查看系统TCP keepalive值

查看系统TCP keepalive值: AIX: $ no -a | grep keep HP-UX and Solaris: $ ndd -get /dev/tcp tcp_keepalive_interval Linux: $ sysctl -a | grep keep AIX/Linux/Solaris/HP-UXAIX查看系统TCP keepalive值

TCP Keepalive HOWTO

TCP Keepalive HOWTO Fabio Busatto <[email protected]> 2007-05-04 Revision History Revision 1.0 2007-05-04 Revised by: FB First release, reviewed by TM. This document describes the TCP keepalive implementation in the linux kernel, introduces the over

为什么基于TCP的应用需要心跳包(TCP keep-alive原理分析)

TCP keep-alive的三个参数 用man命令,可以查看linux的tcp的参数: man 7 tcp 其中keep-alive相关的参数有三个: tcp_keepalive_intvl (integer; default: 75; since Linux 2.4) The number of seconds between TCP keep-alive probes. tcp_keepalive_probes (integer; default: 9; since Linux 2.2)

http keep-alive与tcp keep-alive

http keep-alive与tcp keep-alive http keep-alive与tcp keep-alive,不是同一回事,意图不一样.http keep-alive是为了让tcp活得更久一点,以便在同一个连接上传送多个http,提高socket的效率.而tcp keep-alive是TCP的一种检测TCP连接状况的保鲜机制.tcp keep-alive保鲜定时器,支持三个系统内核配置参数: echo 1800 > /proc/sys/net/ipv4/tcp_keepalive_

TCP连接探测中的Keepalive和心跳包. 关键字: tcp keepalive, 心跳, 保活

1. TCP保活的必要性 1) 很多防火墙等对于空闲socket自动关闭 2) 对于非正常断开, 服务器并不能检测到. 为了回收资源, 必须提供一种检测机制. 2. 导致TCP断连的因素 如果网络正常, socket也通过close操作来进行优雅的关闭, 那么一切完美. 可是有很多情况, 比如网线故障, 客户端一侧突然断电或者崩溃等等, 这些情况server并不能正常检测到连接的断开. 3. 保活的两种方式: 1) 应用层面的心跳机制 自定义心跳消息头. 一般客户端主动发送, 服务器接收后进行回

TCP keepalive under Linux

TCP Keepalive HOWTO Prev   Next 3. Using TCP keepalive under Linux Linux has built-in support for keepalive. You need to enable TCP/IP networking in order to use it. You also need procfs support and sysctl support to be able to configure the kernel p

TCP keepalive

  2. TCP keepalive overview In order to understand what TCP keepalive (which we will just call keepalive) does, you need do nothing more than read the name: keep TCP alive. This means that you will be able to check your connected socket (also known a