Prabhu. S Prabhu. S - 11 months ago 71
C Question


I have used

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

Answer Source

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 transfered.

If two sockets are bound to the same interface and port and 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.


For UDP: SO_REUSEADDR is used for multicast

More than one process may bind to the same SOCK_DGRAM UDP port if the bind() 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.