views:

57

answers:

1

I'm writing a simple HTTP server and learning about TIME_WAIT. How do real web servers in heavy environments handle requests from thousands of users without all the sockets getting stuck in TIME_WAIT after a request is handled? (Not asking about keep-alive -- that would help for a single client, but not for thousands of different clients coming through).

I've read that you try and get the client to close first, so that all the TIME_WAITs get spread out among all the clients, instead of concentrated on the server.

How is this done? At some point the server has to call close/closesocket.

+2  A: 

The peer that initiates the active close is the one that goes into TIME_WAIT. So as long as the client closes the connection the client gets the TIME_WAIT and not the server.

Len Holgate
So does the server just hang around and wait for the client to close? I'd think at some point it would want to time out and close the connection itself.
DougN
Which makes me think of another question :) Do you suppose a real web server (IIS/Apache?) just call select on the socket with a 5(?) second timeout -- figuring that should be plenty of time for the client to disconnect (and let select return with an error socket)?
DougN
If you reset the connection rather than closing it then you don't end up in TIME_WAIT either. So if you wanted to add a timeout for idle connections you'd close them by first turning off linger and then issuing the close. This would send an RST and you wouldnt end up in TIME_WAIT.
Len Holgate