gpt4 book ai didi

c - SO_KEEPALIVE : Set on the server socket or on a per-client basis?

转载 作者:太空宇宙 更新时间:2023-11-04 12:30:31 24 4
gpt4 key购买 nike

我有以下用于为套接字配置 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() 的客户端吗(即 accept() 的客户端会继承这些套接字选项,从而为 TCP Keepalive 配置)?我宁愿不必为每个客户端调用此函数,以最大程度地减少开销。谢谢。

最佳答案

看起来这应该可行。 《Unix网络编程》一书说:

enter image description here

关于c - SO_KEEPALIVE : Set on the server socket or on a per-client basis?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43441913/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com