Closing a TCP listening socket in C

Suppose you have a socket listening on a TCP port and some clients are connected. When one of them issues sock_close (fd) in C and tries to bind it to the same port again, the failure fails. Some TIME_WAIT state is mapped to "netstat -plutnoa", for example:

tcp        0      0 127.0.0.1:4567          127.0.0.1:32977         TIME_WAIT   -                timewait (17.12/0/0)

So, how can you properly disconnect the server socket and reconnect to the same port?

+5
source share
1 answer

You want to use the option SO_REUSEADDRon the socket. Relevant man page socket(7). Here is an example of its use. This question explains what is happening.

+8

All Articles