2

All discussion I've seen on SO_REUSEADDR assumes that it's the same program creating and binding to a TCP socket on a known port.

I have two different programs using the same port, and I'm curious about how the mechanism works -- in order for program 2 to allocate a port program 1 has just closed, do they both have to specify SO_REUSEADDR after they create the socket?

Or is it enough for one of them? If so, the one taking the socket first or the one trying to open it afterwards, when it's lingering in TIME_WAIT state?

Here's a small example in Python to hopefully make the case clearer;

# one.py
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind(("", 5050)) # Assuming 5050 is available
sys.exit(1)        # Assuming s enters TIME_WAIT

# two.py
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind(("", 5050))
s.listen()

Think of one.py and two.py as two separate codebases.

Does both one.py and two.py need to set the SO_REUSEADDR socket option in order for two.py to tolerate a lingering TIME_WAIT socket from one.py?

Thank you.

Kim Gräsman
  • 7,438
  • 1
  • 28
  • 41
  • 1
    http://www.unixguide.net/network/socketfaq/4.5.shtml I guess all your programs should use SO_REUSEADRR. – Jeremy D Dec 14 '11 at 15:26
  • possible duplicate of [Uses of SO_REUSEADDR?](http://stackoverflow.com/questions/577885/uses-of-so-reuseaddr) –  Dec 14 '11 at 15:28
  • @JeremyD -- thanks! I take that to mean that only the second program needs SO_REUSEADDR? It's not entirely clear to me still. – Kim Gräsman Dec 14 '11 at 15:29
  • @VladLazarenko -- I don't think so. I think I understand what SO_REUSEADDR is for, I'm asking if multiple parties need to agree for it to work. – Kim Gräsman Dec 14 '11 at 15:30

4 Answers4

5

To answer your question. I believe that on Linux you have to specify SO_REUSEADDR ONLY in the program that wants to reuse the port. Very simple. On Microsoft Windows however, this is a different story. Microsoft has a page on MSDN that covers SO_REUSEADDR and related features.

dongle26
  • 826
  • 1
  • 10
  • 18
  • Thanks, that MSDN hint was really thorough. I understand more of the background for `SO_REUSEADDR` now, even though I didn't see any mention of the socket state there. Maybe this comes down to empirical testing of different implementations. – Kim Gräsman Sep 23 '12 at 08:40
  • Also, thanks for bringing up portability concerns, I never mentioned I needed a consistent strategy for both Linux and Windows. – Kim Gräsman Sep 23 '12 at 08:41
0
s.setsockopt(socket.SOL_SOCKET, socket.SO_LINGER, struct.pack("ii", 1, 0))

You can abruptly terminate the TIME_WAIT state of TCP negotiation flow by setting SO_LINGER socket option to zero.

Good when you allow two states, ESTABLISHED or nothing.

sz4bo
  • 1
0

I think that yes, all programs using the same port should use SO_REUSEADDR.

Because the timeout for reusing ports is a kernel thing.

(but I may be wrong).

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
0

The system call behind listening sockets is bind(), and you cannot bind to the same port on the same address(es) at the same time: the OS forbids that. SO_REUSEADDR tells that other sockets may listen after the program exits. But as long as it has not exited, it is proprietary of this port.

fge
  • 119,121
  • 33
  • 254
  • 329
  • Yes, this makes more sense after your edit :-). But this is not what I'm asking -- the question is if both programs need to specify SO_REUSEADDR in order for program 2, after program 1 has closed its socket, to be able to open a socket on the same port. – Kim Gräsman Dec 14 '11 at 15:40