Newsgroups: comp.protocols.tcp-ip
Path: utzoo!utgpu!cunews!bnrgate!bwdls61.bnr.ca!pww
From: pww@bnr.ca (Peter Whittaker)
Subject: Re: reuse of addresses when calling bind()
Message-ID: <1990Nov30.202155.8253@bwdls61.bnr.ca>
Sender: usenet@bwdls61.bnr.ca (Use Net)
Organization: Bell-Northern Research, Ltd., Ottawa, Ontario, CANADA
References: <86984@lll-winken.LLNL.GOV>
Date: Fri, 30 Nov 90 20:21:55 GMT

In article <86984@lll-winken.LLNL.GOV> booloo@lll-crg.llnl.gov (Mark Boolootian) writes:
>I've got a meeting in a couple minutes so I'm going to try and ask this quickly.
>If you setsockopt() with SO_REUSEADDR, as I understand it, you should be able
>to issue a bind() for a ip/port address that is already in use.  However, it
>appears that it is still possible for bind() to return the error EADDRINUSE (for
>example, look at routine getdatasock() in ftpd.c where they have a loop that
>retries the bind() several times before giving up, all the while checking for
>EADDRINUSE).  
>
>What I'd like to know is why this happens, in lieu of the call to setsockopt().
>Thanks in advance.  Email would be nice but I'll wade through stuff if need be.

I'm posting instead of e-mailing you directly in order to check my answer against the combined wisdom of the net...

First, the original socket must have set SO_REUSEADDR - you cannot set it 
retroactively.

Second, two independent processes cannot bind to the same port no matter 
what options are set.  SO_REUSEADDR allows the same process, or children of
that process, to bind to same port (this is how ftp works:  one server binds
its children to the same port, so all data flows from the same port).

Finally, under certain conditions (notably the use of SO_KEEPALIVE in 
an HP-UX 6.5 client), connections never disppear when the server dies/exits;
the kernel tells the server that the socket is closed, but never manages to
close it, because the HPUS 6.5 client interferes with the shutdown.  Result:
regardless of any options, the socket cannot reused until the client shuts
down or the machine is rebooted.

--
Peter Whittaker      [~~~~~~~~~~~~~~~~~~~~~~~~~~]   Open Systems Integration
pww@bnr.ca           [                          ]   Bell Northern Research 
Ph: +1 613 765 2064  [                          ]   P.O. Box 3511, Station C
FAX:+1 613 763 3283  [__________________________]   Ottawa, Ontario, K1Y 4H7
