SO_KEEPALIVE:设置在服务器套接字上还是基于每个客户端?

时间:2017-04-16 20:40:57

标签: c linux sockets tcp setsockopt

我有以下功能为套接字配置TCP Keepalive:

int configure_tcp_keepalive(int fd)
{
    int opt_val = 1;
    if (setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &opt_val, sizeof(opt_val)) == -1)
        return -1;

    int keepcnt = 9; //default value on Linux
    if (setsockopt(fd, SOL_TCP, TCP_KEEPCNT, &keepcnt, sizeof(keepcnt)) == -1) 
        return -1;

    int keepidle = 30;
    if (setsockopt(fd, SOL_TCP, TCP_KEEPIDLE, &keepidle, sizeof(keepidle)) == -1) 
        return -1;

    int keepintvl = 30;
    if (setsockopt(fd, SOL_TCP, TCP_KEEPINTVL, &keepintvl, sizeof(keepintvl)) == -1) 
        return -1;
}

如果我在服务器fd上调用此函数,它是否会影响每个accept()客户端(即接受()' d客户端继承这些套接字选项,因此配置为TCP Keepalive) ?我不必为每个客户端调用此函数,以便最大限度地减少开销。感谢。

1 个答案:

答案 0 :(得分:0)

看起来这应该有效。这本书" Unix网络编程"表示:

enter image description here