Search code examples
clinuxtcp

How can I detect TCP dead-connection in linux on C?


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?


Solution

  • 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`
    

    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.

    The keepalive time can be modified at boot time by placing startup script at /etc/init.d.