在服务器端启用 TCP keepalive
这是一个客户端 - 服务器示例。该进程在子进程中的父进程和服务器中分叉并运行客户端:
- 客户端连接到服务器并等待服务器退出;
- 服务器接受来自客户端的连接,启用 keepalive,并等待任何信号。
使用 socket(7)
和 tcp(7)
手册页中描述的以下选项配置 Keepalive :
SO_KEEPALIVE
- 允许发送保持活动消息TCP_KEEPIDLE
- TCP 开始发送 keepalive 探测之前连接需要保持空闲的时间(以秒为单位)TCP_KEEPINTVL
- 各个 keepalive 探测器之间的时间(以秒为单位)TCP_KEEPCNT
- TCP 在丢弃连接之前应发送的最大 keepalive 探测数
源代码:
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/signal.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#define check(expr) if (!(expr)) { perror(#expr); kill(0, SIGTERM); }
void enable_keepalive(int sock) {
int yes = 1;
check(setsockopt(
sock, SOL_SOCKET, SO_KEEPALIVE, &yes, sizeof(int)) != -1);
int idle = 1;
check(setsockopt(
sock, IPPROTO_TCP, TCP_KEEPIDLE, &idle, sizeof(int)) != -1);
int interval = 1;
check(setsockopt(
sock, IPPROTO_TCP, TCP_KEEPINTVL, &interval, sizeof(int)) != -1);
int maxpkt = 10;
check(setsockopt(
sock, IPPROTO_TCP, TCP_KEEPCNT, &maxpkt, sizeof(int)) != -1);
}
int main(int argc, char** argv) {
check(argc == 2);
struct sockaddr_in addr;
addr.sin_family = AF_INET;
addr.sin_port = htons(12345);
check(inet_pton(AF_INET, argv[1], &addr.sin_addr) != -1);
int server = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
check(server != -1);
int yes = 1;
check(setsockopt(server, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) != -1);
check(bind(server, (struct sockaddr*)&addr, sizeof(addr)) != -1);
check(listen(server, 1) != -1);
if (fork() == 0) {
int client = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
check(client != -1);
check(connect(client, (struct sockaddr*)&addr, sizeof(addr)) != -1);
printf("connected\n");
pause();
}
else {
int client = accept(server, NULL, NULL);
check(client != -1);
enable_keepalive(client);
printf("accepted\n");
wait(NULL);
}
return 0;
}
可以使用 tcpdump
监控 Keepalive 数据包。
用法示例:
$ ./a.out 127.0.0.1 &
[1] 14010
connected
accepted
$ tcpdump -n -c4 -ilo port 12345
dropped privs to tcpdump
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on lo, link-type EN10MB (Ethernet), capture size 262144 bytes
18:00:35.173892 IP 127.0.0.1.12345 > 127.0.0.1.60998: Flags [.], ack 510307430, win 342, options [nop,nop,TS val 389745775 ecr 389745675], length 0
18:00:35.173903 IP 127.0.0.1.60998 > 127.0.0.1.12345: Flags [.], ack 1, win 342, options [nop,nop,TS val 389745775 ecr 389745075], length 0
18:00:36.173886 IP 127.0.0.1.12345 > 127.0.0.1.60998: Flags [.], ack 1, win 342, options [nop,nop,TS val 389745875 ecr 389745775], length 0
18:00:36.173898 IP 127.0.0.1.60998 > 127.0.0.1.12345: Flags [.], ack 1, win 342, options [nop,nop,TS val 389745875 ecr 389745075], length 0
4 packets captured
8 packets received by filter
0 packets dropped by kernel