内核提供诸如“ tcp_keepalive_time”,“ tcp_keepalive_intvl”,“ tcp_keepalive_probes”之类的设置...这些探针是否可以自动工作?还是我必须在代码中做一些事情才能真正激活它?
答案 0 :(得分:0)
您需要在套接字上启用这些值。例如在C:
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
void set_tcp_keepalive (int fd)
{
int val;
val = 1;
if (setsockopt (fd, SOL_SOCKET, SO_KEEPALIVE, &val, sizeof (val)) == -1) {
perror ("setsockopt(SO_KEEPALIVE)");
}
val = 60;
if (setsockopt (fd, IPPROTO_TCP, TCP_KEEPIDLE, &val, sizeof (val)) == -1) {
perror ("setsockopt(TCP_KEEPIDLE)");
}
val = 30;
if (setsockopt (fd, IPPROTO_TCP, TCP_KEEPINTVL, &val, sizeof (val)) == -1) {
perror ("setsockopt(TCP_KEEPINTVL)");
}
val = 2;
if (setsockopt (fd, IPPROTO_TCP, TCP_KEEPCNT, &val, sizeof (val)) == -1) {
perror ("setsockopt(TCP_KEEPCNT)");
}
}