help: close(socket) leaves socket bound ?

help: close(socket) leaves socket bound ?

Post by Josh Rabinowi » Tue, 07 Jan 1997 04:00:00



Hello Everyone:

We are developing a tcp/ip client/server application and have the following
problem (SunOS 4.1.x, also observed on other UNIX os'es):
The tcp/ip communication works except for the problem below.

When our server quits, it calls close() on the socket it bind()'ed to.
When the server is restarted, it attempts to re-bind() to the same socket, but
the call returns EADDRINUSE, which means that the specified address is
already in use.
This behavior will continue for about 30-60 seconds or so after the
server has quit,  at which point then the OS seems to silently recognize
that the port is free,  and the bind() magically works again.  I cannot seem to
cause the port to clear and bind() to succeed through any actions of my
own (except to wait!). I know this is not normal server behavior!  

We are doing everything as cleanly as we can; our code is modeled closely
on the TCP/IP socket example in "Using C on the UNIX system" and I have
RTFMs to no avail.  

Have other people experienced this?  Can anyone help us solve this
problem, or offer any pearls of wisdom?

Thanks in advance,
Josh  Rabinowitz

--

 
 
 

help: close(socket) leaves socket bound ?

Post by Michael Marxmeie » Wed, 08 Jan 1997 04:00:00



> When our server quits, it calls close() on the socket it bind()'ed to.
> When the server is restarted, it attempts to re-bind() to the same socket, but
> the call returns EADDRINUSE, which means that the specified address is
> already in use.

Hi Josh.

This can be controlled by a socket option.

int option = 1;
setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &option, sizeof(option));

If SO_REUSEADDR is not set (the default), the kernel will refuse
to bind to a previously port. This is probably to avoid confusion
while a connection attempt may be pending.

Hope this helps
Michael

BTW: You may want to get Richard W. Stevens book "UNIX Network
Programming". I can really recommend it.

--
Michael Marxmeier           Marxmeier Software Entwicklung GmbH

Voice : +49 202 2431440     42119 Wuppertal, Germany

 
 
 

help: close(socket) leaves socket bound ?

Post by Patri » Wed, 08 Jan 1997 04:00:00




Quote:> Hello Everyone:
> Have other people experienced this?  Can anyone help us solve this
> problem, or offer any pearls of wisdom?

Quoted back from the Socket FAQ
-------------------------------
II.7.  Please explain the TIME_WAIT state.
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Remember that TCP guarantees all data transmitted will be delivered, if
at
all possible.  When you close a socket, the server goes into a TIME_WAIT
state, just to be really really sure that all the data has gone through.
When a socket is closed, both sides agree by sending messages to each
other that they will send no more data.  This, it seemed to me was good
enough, and after the handshaking is done, the socket should be closed.
The problem is two-fold.  First, there is no way to be sure that the last
ack was communicated successfully.  Second, there may be "wandering
duplicates" left on the net that must be dealt with if they are
delivered.
-------------------------------------------------------------------------
The FAQ is located at
   http://kipper.york.ac.uk/~vic/sock-faq/
I hope this will help you
Patrick Leung

The Hong Kong Technical College
 
 
 

help: close(socket) leaves socket bound ?

Post by Richard Curno » Wed, 08 Jan 1997 04:00:00


In the server, you need to permit re-use of a port address on the
socket before attempting to bind it.  This gets round the problem that
old connections in the 'TIME_WAIT' state will prevent bind() from
working.

Outline of code (with no checking of O/S return values)

int on=1;
fd = socket(AF_INET, SOCK_STREAM, 0);
setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *) &on, sizeof(on));
bind(fd, &socket_address, sizeof(socket_address));

I'm not sure which header files are needed to support this but grep on
/usr/include/*.h & /usr/include/sys/*.h will probably find them.

--
Richard P. Curnow
Stevenage, England

 
 
 

help: close(socket) leaves socket bound ?

Post by Josh Rabinowi » Fri, 10 Jan 1997 04:00:00


Thanks to all for their help. THe solution is to call
SO_REUSEADDR as shown below.

The original question:
"When our server quits, it calls close() on the socket it bind()'ed to.
When the server is restarted, it attempts to re-bind() to the same socket, but
the call returns EADDRINUSE, which means that the specified address is
already in use.  How should we handle this?"

joshr



> long on=1;
> fd = socket(AF_INET, SOCK_STREAM, 0);
> setsockopt(fd, SOL_SOCKET,SO_REUSEADDR, (char *) &on, sizeof(on));
> bind(fd, &socket_address, sizeof(socket_address));

--

 
 
 

1. Sockets: server close leaves socket bound?

I have a server listening on a bound socket.  I accept a connection.
No matter what I do at this point, if the server closes the connection
before the client it leaves the socket in a messy state and I have to wait
for the system to clean it up before another process can bind to that socket
again.

Is there something that I should be doing aside from close(fd) ?

Pat

2. Setting up a relay

3. Reclaiming used socket fd or how to close sockets properly

4. Annoying non-feature of Gnome

5. How to close left-open sockets

6. It MUST could be done

7. Bind: socket operation on non-socket

8. 2.1/2.2SNAP and Triton II motherboards?

9. bind: socket operation on non-socket

10. socket closed but still Unable to bind.

11. Sockets sockets and more sockets

12. Socket, Socket, whose got the socket?

13. Socket, socket, who's got the socket?