我正在设计一个 TCP 服务器,我需要删除 half-opened connections尽可能快。每隔n
秒我尝试向客户端发送一些数据,当我收到错误时,我停止轮询它们。
它工作得很好,除了我的客户收到一些我每n
秒发送给他们的垃圾并且必须将其解析出来。
我想发送一个包含零字节数据的 TCP 段。是否可以修改 TCP 段 header 并发送某种没有数据的假 SYN
或 ACK
段?
最佳答案
As far as I know system keep-alive timeout on linux is two hours which is way to much and is a system-wide setting which can be overwritten by other applications if I change it to e.g. one minute. – Kolyunya
您还可以在其中设置每个套接字的保活选项。
int true = 1;
int idle = 60;
if (setsockopt(s, SOL_SOCKET, SO_KEEPALIVE, &true, sizeof true) < 0
|| setsockopt(s, IPPROTO_TCP, TCP_KEEPIDLE, &idle, sizeof idle) < 0
|| setsockopt(s, IPPROTO_TCP, TCP_KEEPINTVL, &true, sizeof true) < 0 /* 1 s */
) perror("setsockopt");
关于c - 发送带有修改报头的 TCP 段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20556823/