I have used SO_REUSEADDR
to have my server which got terminated to restart without complaining that the socket is already in use. I was wondering: are there other uses of SO_REUSEADDR
? Has anyone used the socket option for other than said purpose?

- 19,179
- 10
- 84
- 156

- 1,743
- 2
- 21
- 34
2 Answers
For TCP, the primary purpose is to restart a closed/killed process on the same address.
The flag is needed because the port goes into a TIME_WAIT
state to ensure all data is transferred.
If two sockets are bound to the same interface and port, and they are members of the same multicast group, data will be delivered to both sockets.
I guess an alternative use would be a security attack to try to intercept data.
(Source)
For UDP, SO_REUSEADDR
is used for multicast.
More than one process may bind to the same
SOCK_DGRAM
UDP port if thebind()
is preceded by:int one = 1; setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
In this case, every incoming multicast or broadcast UDP datagram destined to the shared port is delivered to all sockets bound to the port.
(Source)

- 15,395
- 32
- 113
- 196

- 339,232
- 124
- 596
- 636
-
3Clarification required. A listening port doesn' go into TIME_WAIT, but accepted connections might. TIME_WAIT isn't there to ensure all data is transferred, it is there to avoid confusion with a subsequent connection between the same end points. – user207421 Jan 02 '13 at 06:32
-
Is the 'bind' happening on the sender side OR the receiver side (for UDP: SO_REUSEADDR)? What happens if the sender and receiver are separate nodes on the network? – Sammy Jan 26 '18 at 11:10
-
@Sammy The receiver (server) does the `SO_REUSEADDR` + `bind()` on a UDP _IP.:port_. The sender (client) `bind()` + `sendmsg()` or just `sendto()` to send messages to the receiver. For back and forth communication, both computers create a socket to receive (so both act like a server, even the client). If you're used to TCP, this is definitely confusing. If you have separate nodes (computers), then the `SO_REUSEADDR` is not useful since you can have just one listener per computer. – Alexis Wilke Mar 14 '22 at 16:20
The other main use is to allow multiple sockets to bind()
to the same port on UDP. You might not think that would come up, but sometimes multiple apps may want to listen on broadcast/multicast addresses with a given port number. It also allows one to bind to the wildcard address, while also binding to a specific address. For instance, Apache might bind to *:80 and 10.11.12.13:80

- 24,196
- 7
- 44
- 55
-
"It also allows one to bind to the wildcard address, while also binding to a specific address" — this contradicts `man 7 socket` and empirical testing with `socat`. – claymation Feb 09 '23 at 18:29