如何检测TCP在Linux基于C死连接? [英] How can I detect TCP dead-connection in linux on C?
问题描述
我写了一篇关于C,其中客户端发送一次性一些信息到服务器的程序。我使用的TCP sockets.And一些时间,服务器计算应该对发送结果到客户端。我如何检测服务器或客户端上的连接被打破?
I wrote a program on C , where a client sent one time some information to a server. I used TCP sockets.And some time the server calculated and should to sent result to the client. How can I detect if connection on The server or the client was broken?
推荐答案
您可能想尝试的TCP保持连接。
You may want to try TCP keepalives.
# cat /proc/sys/net/ipv4/tcp_keepalive_time
7200
# cat /proc/sys/net/ipv4/tcp_keepalive_intvl
75
# cat /proc/sys/net/ipv4/tcp_keepalive_probes
9`
在上面的例子中,TCP保持活定时器踢7200秒的空闲时间之后。如果保活消息是不成功然后它们在75秒的时间间隔重试。经过连续9重试失败,连接将被击落。
In the above example, TCP keep-alive timer kicks in after the idle time of 7200 seconds. If the keep-alive messages are unsuccessful then they are retried at the interval of 75 seconds. After 9 successive retry failure, the connection will be brought down.
的存活时间可以在启动时通过将启动脚本在/etc/init.d中进行修改。
The keepalive time can be modified at boot time by placing startup script at /etc/init.d.
这篇关于如何检测TCP在Linux基于C死连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!