Problem with binding of socket addresses

Jonathan I. Kamens jik at athena.mit.edu
Tue Dec 11 06:41:30 AEST 1990


In article <epeterso.660257641 at houligan>, epeterso at houligan.encore.com (Eric Peterson) writes:
|> However, the server occasionally hits a bug and core dumps or dies off
|> in some other way.  But it dies off and closes its end of the
|> connection before the client closes the other end.  When this occurs
|> and I attempt to restart the server, the bind() call fails with the
|> error "Address already in use".
|> 
|> Now, neither the client nor the server is running at the time I try to
|> restart the server, and there isn't a problem with address collisions
|> with another process.  As far as I can tell, nothing else is using
|> this address.  So why does bind() fail?

In article <sean.660642165 at s.ms.uky.edu>, sean at ms.uky.edu (Sean Casey) writes:
|> Set the "reuse address" socket option, between the socket() and the
|> bind() calls. Then your program can always immediately restart.

  Sean's suggestion will solve the problem, but he does not explain why the
problem occurs, so I guess I'll do that :-).

  The TCP protocol states that after a TCP stream connection has been closed
abnormally, the same local/foreign port combination cannot be used again for
(2 * MSL).  MSL stands for the Maximum Segment Lifetime, which is usually set
to a minute, which means that it probably takes about two minutes before the
address is useable again.

  The reason for this is to make sure that all packets which were supposed to
get to the old process connected to the socket don't accidentally get
delivered to the new process instead -- the delay is long enough so that
all the waiting packets should ge thrown away.

  Using the reuse address socket option will make it possible for you to
rebind to the socket.  It's also a violation of the TCP protocol.  But what
the hell, sometimes pragmatism has to win out over theory.  This is definitely
one of those times :-).

-- 
Jonathan Kamens			              USnail:
MIT Project Athena				11 Ashford Terrace
jik at Athena.MIT.EDU				Allston, MA  02134
Office: 617-253-8085			      Home: 617-782-0710



More information about the Comp.unix.programmer mailing list