I'm reading the doc about socat here and here.
The example socat -u TCP4-LISTEN:3334,reuseaddr,fork OPEN:/tmp/test.log,creat,append
works well with and without option reuseaddr
.
What does the reuseaddr
do?
Why above example works well with and without the reuseaddr
?
In which cases the reuseaddr
is really needed?
reuseaddr
allows to bind to address which is in TIME_WAIT
state, e.g. after server is killed while client is connected.
Without reuseaddr
, server side:
$ socat -u TCP-LISTEN:3334,fork -
Client:
$ socat -u TCP-CONNECT:localhost:3334 -
Back to server:
^C
$ netstat -nta | grep 3334
tcp 0 0 127.0.0.1:3334 127.0.0.1:37578 TIME_WAIT
$
$ socat -u TCP-LISTEN:3334,fork -
2023/01/14 16:44:19 socat[84239] E bind(5, {AF=2 0.0.0.0:3334}, 16): Address already in use
With reuseaddr
(before run wait until TIME_WAIT
state passed away, or use different port number):
$ socat -u TCP-LISTEN:3334,fork,reuseaddr -
Client:
$ socat -u TCP-CONNECT:localhost:3334 -
Back to server:
^C
$ netstat -nta | grep 3334
tcp 0 0 127.0.0.1:3334 127.0.0.1:35168 TIME_WAIT
$ socat -u TCP-LISTEN:3334,fork,reuseaddr -
^^^ No error.
Note reuseaddr
should be applied to both initial and subsequent sockets.