我正在尝试设置tcp keepalive,但这样做时我看到了错误

“协议(protocol)不可用”

int rc = setsockopt(s, SOL_SOCKET, TCP_KEEPIDLE, &keepalive_idle, sizeof(keepalive_idle));
if (rc < 0)
    printf("error setting keepalive_idle: %s\n", strerror(errno));

我可以打开keepalive,设置keepalive间隔和计数,但keepalive空闲(这是keepalive时间)会引发该错误,而且我从未见过使用wireshark和过滤器tcp.analysis.keep_alive或tcpdump发送/接收任何keepalive数据包
sudo tcpdump -vv "tcp[tcpflags] == tcp-ack and less 1"

是否有需要加载的内核模块?还是您不再能够覆盖全局KEEPIDLE时间。

顺便说一下
matt@devpc:~/ sysctl net.ipv4.tcp_keepalive_time net.ipv4.tcp_keepalive_probes net.ipv4.tcp_keepalive_intvl
net.ipv4.tcp_keepalive_time = 7200
net.ipv4.tcp_keepalive_probes = 9
net.ipv4.tcp_keepalive_intvl = 75

最佳答案

在我编写的应用程序中,以下工作正常:

setsockopt(*sfd, SOL_SOCKET, SO_KEEPALIVE,(char *)&enable_keepalive, sizeof(enable_keepalive));
setsockopt(*sfd, IPPROTO_TCP, TCP_KEEPCNT, (char *)&num_keepalive_strobes, sizeof(num_keepalive_strobes));
setsockopt(*sfd, IPPROTO_TCP, TCP_KEEPIDLE, (char *)&keepalive_idle_time_secs, sizeof(keepalive_idle_time_secs));
setsockopt(*sfd, IPPROTO_TCP, TCP_KEEPINTVL, (char *)&keepalive_strobe_interval_secs, sizeof(keepalive_strobe_interval_secs));

尝试将SOL_SOCKETIPPROTO_TCP更改为TCPKEEPIDLE

关于linux - tcp keepalive-协议(protocol)不可用?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/23669005/

10-10 06:43