I am sure that cleaning the socket is more of a side effect of freeing all file descriptors after the process freezes, rather than being performed directly by cleaning the process.
I'm going to go to a limb, and suppose you fall into a common trap with network programming. If I correctly understood that your problem is that you get the "Address in Use" (EADDRINUSE) error when trying to bind to the address after the process has been killed, you are launched into the TIME_WAIT socket.
If so, you can either wait for a timeout, usually 60 seconds, or you can change the socket to use reuse immediately.
int sock, ret, on; struct sockaddr_in servaddr; sock = socket( AF_INET, SOCK_STREAM, 0 ): on = 1; ret = setsockopt( sock, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on) );
[EDIT]
From your comments. It seems like you have problems with half-open connections, and I don't quite understand how TCP works. TCP has no way of knowing if the client is dead or just idle. If you kill -9 client process, the four-way handshake closure never ends. This should not leave open connections on your server, so you may need to get a network dump to be sure what is happening.
I can’t say exactly how you should handle this without knowing exactly what you are doing, but you can read TCP Keepalive here . A couple of other parameters periodically send an empty or null message to the client (it may require changing your protocol) or set hard timers during connection downtime (may lead to the rejection of actual connections).
Jimb
source share