LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Networking (https://www.linuxquestions.org/questions/linux-networking-3/)
-   -   linux programing: checking of socket connection is alive (https://www.linuxquestions.org/questions/linux-networking-3/linux-programing-checking-of-socket-connection-is-alive-902462/)

tilman1 09-11-2011 02:09 PM

linux programing: checking of socket connection is alive
 
Hello

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 ?

2)
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 lcu1.fritz.box:38397 SYN_RECV
tcp 0 0 ztron:9600 lcu1.fritz.box:38396 SYN_RECV
tcp 17 0 ztron:9600 lcu1.fritz.box:38479 CLOSE_WAIT
tcp 17 0 ztron:9600 lcu1.fritz.box:38395 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:

Code:

if (send(client->fd, buffer_tx,strlen(buffer_tx), 0) < 0)
{
  close(client->fd);
  client->fd=-1;
 ...
}

Thanks

Tilman

marco18 09-11-2011 08:51 PM

I believe this thread corresponds to http://www.linuxquestions.org/questions/programming-9/

You will find proper help there.


All times are GMT -5. The time now is 08:51 AM.