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