-   Linux - Networking (
-   -   linux programing: checking of socket connection is alive (

tilman1 09-11-2011 03:09 PM

linux programing: checking of socket connection is alive

I am writing a daemon that is servecing TCP-socket connections. I want the communication to reliable, i.e. if the connection breaks for whatever reason, I want the daemon to just wait for the next connection, and the client to automatically reconnect.

How do I do this ?

1) Do I need to close and reopen the socket, on which I am listing for inbound connection request ?

a) If I kill the client, the server either terminates, or receives garbage. Sometimes it works fine, i.e. I kill the client, restart the client, and it reconnects just fine

The failing behavior might have something to do with the socket not being closed yet (daemon listens on port 9600):

netstat -t
tcp 0 0 ztron:9600 SYN_RECV
tcp 0 0 ztron:9600 SYN_RECV
tcp 17 0 ztron:9600 CLOSE_WAIT
tcp 17 0 ztron:9600 CLOSE_WAIT

b) If I kill the server, the client often subsequently terminates as well. The exit code is 141 which is none of the arguments given to exit statements. The program either exits with exit(0) or exit(-1).

I guess the issue is that neither the server or the client realize that there is nobody on the other end of the socket any longer.

How do I find out about it ?
Right now, I am checking every send and recv command for a negative return value like this:


if (send(client->fd, buffer_tx,strlen(buffer_tx), 0) < 0)



marco18 09-11-2011 09:51 PM

I believe this thread corresponds to

You will find proper help there.

All times are GMT -5. The time now is 08:39 PM.