EDIT: tl;dr:
Don't open sockets with the wrong options, it's unlikely to work!
Original Question:
I'm working with a program which doesn't have a clean shutdown mechanism, it just relies on being killed to terminate it.
It opens a socket as follows:
(void) setsockopt(h, SOL_SOCKET, (SO_KEEPALIVE | SO_REUSEADDR), (int *) & optval, sizeof( optval ));
The re-use address option does seem to work, in that eventually the socket is released after the program dies. Eventually being anything from a couple of seconds up to maybe a minute.
This is quite tedious as I need to restart this program regularly. Making the program teminate properly would be a very big job, but I'm wondering if there's anything more local I can do to release the socket earlier?
Socket options are not bits and hence can not be or'ed like SO_KEEPALIVE | SO_REUSEADDR. To be pedantic, SO_KEEPALIVE | SO_REUSEADDR yields another socket option SO_NO_CHECK.
You don't notice this mistake because you don't check the return value of setsockopt. Now you know why ignoring return values is considered bad practice.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With