views:

30

answers:

2

I have been trying to implement the TCP_KEEPALIVE parameter for a C server socket and I can't seem to figure out how to check if the socket is marked as broken. I followed this tutorial for configuring the socket to do the keep alive but it says that "If no ACK response is received for nine consecutive times, the connection is marked as broken". My question is, how do I know the socket is marked as broken?

Tutorial: http://tldp.org/HOWTO/html_single/TCP-Keepalive-HOWTO/

+1  A: 

read() from the socket.

read will return -1 if the socket has been broken.

nos
Also, a return value of 0 from `recv` indicates an orderly shutdown.
Jack Kelly
+1  A: 

If the kernel decides the connection is broken, you'll get an error when you try to send to or receive from the socket. You should already be doing the appropriate error handling around sends and receives to handle the "normal" case where your peer spontaneously terminates the connection. So, no "extra" error handling code should be required.

Kaelin Colclasure