SUMMARY: "Address already in use"

From: Steve Campbell (steve@avalon.dartmouth.edu)
Date: Fri Feb 26 1993 - 13:43:46 CST


Original query:

> We have a Sun 4/280 running SunOS 4.1. It's a busy system; load
> averages of 20 and above are not uncommon. One of the applications on
> it is one we wrote that opens a TCP stream connection to another of our
> hosts. The code is straightforward: it looks up the host and port
> (902) using get*byname(3), does a socket(2) and a connect(2). There
> tend to be 50 to 100 instances of this application running at any given
> moment, and over the course of the day it probably runs thousands of
> times. Most of the time it works fine, but a handful of times a day
> the connect(2) fails with the error code EADDRINUSE - address already
> in use. I can't figure out why. I'd be grateful if anyone has clues.

There were about 12 relies. The most frequent suggestion was to use
setsockopt(2) to set the SO_REUSEADDR option on the socket. I tried
this, but it did not solve the problem. I believe that the reason is that
this option has meaning for servers that do a bind(2) and a listen(2),
but not for clients like my program that do a connect(2).

Another respondent suggested setting the socket option SO_DONTLINGER.
I modified my program to use getsockopt to find the linger value that is
initially in effect and found it is zero. I concluded that setting
the linger to zero would have no effect on my program.

Andy Lynn, maximus!andy@uunet.UU.NET, suggested using bzero(3) to clear
my sockaddr structure before putting anything into it. This is a good idea
since my struct is an automatic variable, and I left this change in.
But it did not get rid of the EADDRINUSE errors.

Glenn Satchell down in Sydney, glenn%ups.uucp@fourx.Aus.Sun.COM, and
Frederic Hemmer in Geneva, hemmer@sun2.cern.ch, pointed me to patch
100159-01 with synopsis "SunOS 4.1, 4.1.1:system hangs using sockets in
local loopback tcp-ip." I applied this patch and have not seen the error
condition for 24 busy hours. So I think that did it. (Version 4.1.2 fixed
the bug.) Let's hear it for the worldwide electronic village.

A postscript: Kumaraswamy Subramanian <Subramanian.Kumaraswamy.1@nd.edu>
referred me to "UNIX Network Programming" by W. Richard Stevens, published
by the Prentice Hall Software Series. It's expensive, but it's an
excellent book. I recommend it.

                                                Steve Campbell
                                                Dartmouth College
                                                Hanover, NH, USA



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