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):
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:
if (send(client->fd, buffer_tx,strlen(buffer_tx), 0) < 0)