SUMMARY: Berkeley Sockets Problems - "local address already in use"

From: Amir J. Katz (amir@matis.ingr.com)
Date: Sun Jun 20 1993 - 04:54:14 CDT


[I have posted the query to comp.unix.questions, but the summary may be of
 use/interest to this newsgroup/mailing-list (or so I hope :-)]

Recetly I asked:

>> I am following the recommended sequence of creating a
>> connection-oriented protocol between a client and a server (as
>> detailed in section 6.2 in Stevens' book UNIX NETWORK PROGRAMMING),
>> that is, socket(), bind(), listen() and accept(), then close() on the
>> socket when it's no longer needed. The sockets are Internet Domain
>> sockets, and STREAM.
>> The problem that I'm having is when I kill the server, and try to
>> restart, bind() returns with the error, "Address already in use". If I
>> wait a few minutes, the address is released and I can restart the
>> server. I added a signal handler that closes the socket that has been
>> opened by accept(), but it does not always help.
>>
>> My questions are:
>> 1. Is there a Unix command to show all ports being used, and by whom?
>> 2. What's the opposite of bind(), that is, how do I "unbind" and
>> address so I don't get this error?
>> 3. Or, what should one do to handle signals when dealing with sockets?
>>
>> Please note that I'm not concerned with loss of data due to a signal,
>> only with the port binding problem.

I later added:

>> Addendum: The first response I got suggested that I use 'setsockopt'
>> with the flag SO_REUSEADDR. I forgot to mention that I already did
>> that, with the same results, that is, I still get the problem.

The Net comes through once again! I got flooded with responses. Special
thanks to W. Richard Stevens, who stopped preparing the second edition of the
Networking Bible to answer my question (:-).

Here are the (almost unanymous) answers:

1. Use 'netstat -a [-f inet]'.
   Amir's note: I was using netstat for routing info and it did not occur to me that it
   can do sockets.
2. There is no "unbind()". The solution is to call setsockopt() with
   SO_REUSEADDR BEFORE calling bind(), not after.
3. No coherent response on this issue.

Here are the winners (no offense to others):

-------------------------------------------------------------------------
Michael Procter <xprocter%teaching.physics@ox.ac.uk> said:

>> The problem is not with unbinding, but the fact that the socket is left
>> in a TIME_WAIT state for a period to try to remove the chances of
>> contention. One way to get rid of this problem might be to change
>> the options on the socket.

-------------------------------------------------------------------------
W. Richard Stevens <rstevens@noao.edu> [The author of the Networking Bible]
said:

>> You are running into the TCP prototocl spec. The side that does the
>> active close (first "close") must wait for twice the MSL after receiving the
>> FIN from the other end. This is normally not a problem, since clients
>> usually do the active close, and client's bind ephemeral ports. But, when
>> you start killing servers, and then restarting them, they hit this when they
>> try to bind their well-known port.
>>
>> RFC 1122 and RFC 793 say the MSL should be 2 minutes, but I find many
>> systems (4.3+BSD, SunOS 4.x, AIX 3.2) use 30 seconds, giving a 2MSL value of
>> 60 seconds. (Solaris 2.1 is the first I've found that really uses 120
>> seconds for the MSL.)
>>
>> setsockopt of SO_REUSEADDR, done *before* the bind. You said in a later
>> posting that this didn't work. You probably weren't doing it before the
>> bind when the server was restarted.
>>
>> p.s. -- This *will* be in the next edition of UNP ...

-------------------------------------------------------------------------
Charles E. Campbell <cec@gryphon.gsfc.nasa.gov> said:

>> You will likely find that the Simple Sockets Library is extremely
>> helpful to you for this particular problem. It has a "PortMaster"
>> which runs in the background and allows one to give servers names. The
>> port to which a server name is bound to is assigned randomly by the system;
>> ie. an *available* port will be assigned by the Sopen call. In fact, with
>> the SSL one doesn't even need to know the port number.
>>
>> Now, for the SSL blurb:
>>
>> It has been tested on: Unix (SGI, DecStation, AIX, Sun 3 and Sparcstation),
>> VMS, and MSDOS (client only since there's no background there). It is
>> provided in source code form, of course, and sits atop Berkeley sockets and
>> tcp/ip.
>>
>> You can order the "Simple Sockets Library" from
>>
>> Austin Code Works
>> 11100 Leafwood Lane
>> Austin, TX 78750-3464 USA
>> Phone (512) 258-0785
>> Fax (512) 258-1342
>>
>> Ask for the "SSL - The Simple Sockets Library". Last I checked, they were
>> asking $20 US for it.

[Amir's note: Charles Campbell's mail is longer and contain examples of
using the SSL. If anyone is interested in his full reply, I can email it]

-------------------------------------------------------------------------
Barry Margolin <barmar@Think.COM> said:

>> The netstat command will show all the ports being used, but it doesn't show
>> by whom. I think the PD program ofiles may be able to show who is using a
>> particular port. However, in the situation you describe, it won't help,
>> because the server that was using the port is already gone.
>>
>> There is no unbind. The problem you're having is due to the fact that TCP
>> requires that the ports used by a connection not be reused for a few
>> minutes after it has closed, so that delayed or duplicated packets for that
>> connection can be recognized. This causes bind() to disallow creating
>> servers for that port by default. You can override this by setting the
>> SO_REUSEADDR socket option before binding.

-------------------------------------------------------------------------
Dr. Laurence Leff <mflll@uxa.ecn.bgu.edu> said:

>> If you are using the UNIX domain, you can often get this error when the
>> "file" that corresponds to the socket name already exists. One often
>> has to remove this file manually.
>>
>> If you are using an INTERNET domain, you mighthave the problems discussed
>> by the other poster.
-------------------------------------------------------------------------

Thanks to:
----------
Michael Procter xprocter%teaching.physics@ox.ac.uk
Stefan Stille) stille@ibr.cs.tu-bs.de
W. Richard Stevens rstevens@noao.edu
Paul Danckaert (ACS) pauld@umbc.edu
Charles E. Campbell cec@gryphon.gsfc.nasa.gov
Barry Margolin barmar@Think.COM
Bev Schwartz bschwart@BBN.COM
Eric E. Johnson) ejohnson@ncsa.uiuc.edu
gaja vaidyanatha gvaidya@andy.bgsu.edu
Joe Ogulin jogulin@apollo.stevens-tech.edu
Michael Kenney kenney@u.washington.edu
Dr. Laurence Leff mflll@uxa.ecn.bgu.edu

-- 
/* ------------------------------------------------------------------- */
/*  Amir J. Katz         | amir@matis.ingr.COM  (or ingr!matis!amir)   */
/*  System Specialist    | Voice: +972 9-584684                        */
/*  SEE Technologies Ltd.| Fax:   +972 9-543917                        */
/*  ......... To Boldly Go Where No One Has Hacked Before ............ */



This archive was generated by hypermail 2.1.2 : Fri Sep 28 2001 - 23:07:56 CDT