Avoidance TIME_WAIT

I am trying to avoid TIME_WAIT in the client. I connect and then set O_NONBLOCK and SO_REUSEADDR. I call read until it returns 0. When read returns 0, errno is also 0. I interpreted this as a sign that the server had closed the connection. However, if I'm close, the socket is set to TIME_WAIT, as confirmed by netstat.

Since I make several connections to the same host / port, I end up starting to see "Addresses in use" errors (see http://hea-www.harvard.edu/~fine/Tech/addrinuse.html )

Should I call close after read returns 0? If I do not give a file descriptor?

+5
source share
3 answers

The party that initiated the closure of the connection is the one that ends in the state TIME_WAIT. read()return 0 should indicate that the server was the first to close the socket, so yes - this should mean that it TIME_WAITends on the server side, and the client goes through LAST_ACK.

At the end of the day you cannot escape the condition TIME_WAIT. Even if you manage to transfer it from the client to the server, you still cannot reuse this tuple (server host, server port, client host, client port)until TIME_WAITit ends (regardless of which side it is on).

Since the three parts of the tuple are fixed in your script ( server host, server port, client host), you really only have these options:

  • . " " ​​ ( OSX ). , , , , bind()/connect() , .

  • client host , IP- . bind() IP-, .

  • server host/server port, / IP- . (, ..).

  • , , : , , " ", , (, HTTP keep-alive).

+5

SO_REUSEADDR. , . , .

+1

SO_REUSEADDR , SO_REUSEADDR

0

All Articles