Socket Addr Re-use - help!

Socket Addr Re-use - help!

Post by Greg Herle » Sun, 08 Dec 1996 04:00:00



I am developing an app that uses sockets, and trying to handle errors
as gracefully as possible:  I ignore SIGPIPE and watch the actual
reads/writes for problems...  on detecting that the other end closed
the connection, I close my end...

Then, I re-create the socket and try to bind it.  Here's where the
problem happens:  I get an error telling me the address is already in
use.  I am seting the socket option SO_REUSEADDR, which I thought
would prevent this error...

Any help would be appreciated.

--
Greg Herlein

http://www.crl.com/www/users/gh/gherlein
Vallejo, CA

 
 
 

Socket Addr Re-use - help!

Post by stanislav shalun » Mon, 09 Dec 1996 04:00:00


 greg> I am developing an app that uses sockets, and trying to handle
 greg> errors as gracefully as possible: I ignore SIGPIPE and watch the
 greg> actual reads/writes for problems...  on detecting that the other
 greg> end closed the connection, I close my end...

That's best you can do.

 greg> Then, I re-create the socket and try to bind it.  Here's where
 greg> the problem happens: I get an error telling me the address is
 greg> already in use.  I am seting the socket option SO_REUSEADDR,
 greg> which I thought would prevent this error...

Your client has closed socket ungracefully (most probably it got
killed).  And connection still exists even though you did everything
right.  It's in TIME_WAIT state: the point behind TIME_WAIT is that
there might be some data pending, not transmitted yet to your system
from the connection it had---then if it allows you to bind to the
address in TIME_WAIT state, you'd get data that is not intended for this
connection.

You can either: (i) wait about 2 minutes (ii) use other port (iii) have
your client be more careful about closing socket.

--

PGP key fingerprint = 62367EA3D4D01D88E021F40A0D938E12 | public key on www page
Opinions that might have been represented here are just mine, not anyone else's
                To err is computer's, to forgive--human.

 
 
 

Socket Addr Re-use - help!

Post by Andrew Gier » Mon, 09 Dec 1996 04:00:00


 Greg> I am developing an app that uses sockets, and trying to handle errors
 Greg> as gracefully as possible:  I ignore SIGPIPE and watch the actual
 Greg> reads/writes for problems...  on detecting that the other end closed
 Greg> the connection, I close my end...

 Greg> Then, I re-create the socket and try to bind it.  Here's where the
 Greg> problem happens:  I get an error telling me the address is already in
 Greg> use.  I am seting the socket option SO_REUSEADDR, which I thought
 Greg> would prevent this error...

Is this in a client or a server? If a server, did you also close the
original listening socket?

SO_REUSEADDR does not allow you to bind a port that is currently in use by
an *unconnected* stream socket.

--

"Ceterum censeo Microsoftam delendam esse" - Alain Knaff in nanam

 
 
 

Socket Addr Re-use - help!

Post by Andrew Gier » Mon, 09 Dec 1996 04:00:00


 greg> Then, I re-create the socket and try to bind it.  Here's where
 greg> the problem happens: I get an error telling me the address is
 greg> already in use.  I am seting the socket option SO_REUSEADDR,
 greg> which I thought would prevent this error...

 stanislav> Your client has closed socket ungracefully (most probably
 stanislav> it got killed).  And connection still exists even though
 stanislav> you did everything right.  It's in TIME_WAIT state: the
 stanislav> point behind TIME_WAIT is that there might be some data
 stanislav> pending, not transmitted yet to your system from the
 stanislav> connection it had---then if it allows you to bind to the
 stanislav> address in TIME_WAIT state, you'd get data that is not
 stanislav> intended for this connection.

Ahemm. Engage brain before posting to Usenet (or read the damn FAQ).

1. Connections end up in TIME_WAIT state *because* they were closed
cleanly.

2. Binding a socket to a port number associated with a connection in
TIME_WAIT state *DOES NOT* cause you to receive data from the old
connection. I don't know where this particular myth comes from.

3. By setting SO_REUSEADDR, you can bind() the port anyway; this is
the Right Thing to do for servers using fixed port numbers.

The sockets FAQ can be found, as usual, at:

   http://www.auroraonline.com/sock-faq/
   http://kipper.york.ac.uk/~vic/sock-faq/
   ftp://rtfm.mit.edu/pub/usenet/news.answers/unix-faq/socket

--

"Ceterum censeo Microsoftam delendam esse" - Alain Knaff in nanam