From: thomson@hub.toronto.edu (Brian Thomson)
Newsgroups: comp.protocols.tcp-ip
Subject: Re: Question on bind-socket
Date: 10 Apr 92 01:00:52 GMT
Organization: University of Toronto

In article <1992Apr9.151941.25074@icd.ab.com> aep@icd.ab.com (Alexander E. Pensky) writes:
>In article <1992Apr8.191115.26347@jarvis.csri.toronto.edu>, thomson@hub.toronto.edu (Brian Thomson) writes:
  [ concerning an example where two sockets are created and bound to the
    same address ]
>> The second bind will fail whether or not SO_REUSEADDR is specified,
>> even if it is specified for the correct (the second) socket.
>> The only time you may have two sockets with exactly the same local and
>> foreign addresses and ports is when they are both "brand new" and 
>> completely unbound.
>
>Sorry, Brian, this is not correct either.  If SO_REUSEADDR is specified,
>you may have two sockets bound to the same local address, as long as their
>remote addresses are different.

Yes, that is exactly what I said.  Are you confused because I used the
word "foreign" instead of "remote"?  Or did you forget that the original
question did not involve any connect() calls, and so the foreign addresses
could not have been bound?

This has all gone on far longer than it should have, and I apologize to
all you long-suffering readers for having yet another teeter at the totter,
but I will include here (in my final bleat on the subject!) an explanation
of SO_REUSEADDR that I concocted some time ago.  It refers to "wildcards",
which are addresses or ports that have not yet been bound (either through
bind() or connect(), as appropriate), or and address that has been explicitly
set to INADDR_ANY, all of which 'netstat' represents with an asterisk:

--------------------------------------------
If you try to bind a socket, and you specify an explicit port number
in the bind(), and the socket is either non-TCP or is not in LISTEN state,
and the socket does not have SO_REUSEADDR, the system will complain if
the result would clash with an existing socket, interpreting the wildcards
as wild for matching purposes.

If any of those conditions are not met, eg. if SO_REUSEADDR has been set,
or it is a UDP socket, or a TCP socket that has been listen()'ed,
the system will complain only if there exists another socket
with the exact same local + remote addresses and ports. "Exact" means
that if one is a wildcard, the other also has to be a wildcard in
order to clash.


						Bindings
Eg,					local		foreign
Step 1: create a TCP socket             *.*             *.*         CLOSED

Step 2:bind() it:                       *.11992         *.*         CLOSED
 I specified INADDR_ANY for the
 address, and 11992 for the port

Step 3: listen()                        *.11992         *.*         LISTEN

Step 4:accept connection                *.11992         *.*         LISTEN
 This creates a second socket   localhost.11992 localhost.2345      ESTABLISHED
 bound as shown

Step 5:close the listener       localhost.11992 localhost.2345      ESTABLISHED

If I try to bind another socket to
                        *.11992         *.*
without REUSEADDR, the wildcards will match the ESTABLISHED socket and
the bind() call will fail.

With REUSEADDR, I will succeed unless there is another socket already
around with the *exact same bindings*, i.e. unless I didn't close the
old listener socket.
-- 
		    Brian Thomson,	    CSRI Univ. of Toronto
		    utcsri!uthub!thomson, thomson@hub.toronto.edu
Newsgroups: comp.protocols.tcp-ip