Subj : Re: Closing tcp socket connections To : comp.programming From : Martin Hierholzer Date : Mon Aug 15 2005 04:40 pm Alex Fraser wrote: >> The protocol between client and server is very simple, for every data >> packet sent by the client a new connection will be opened, the client >> will send its data and the server sends a reply and should close the >> connection. > > This is very inefficient, it would be better to maintain connections. I know, but this is acceptable. The data rate in production environment will be very low. Keeping connections open is not so easy in this case... (e.g. it could mean, that the connections would stay open for 5 years!) >> But the connections seem to stay open even after these calls. Running >> netstat even immediately after cancelling the test client shows a lot of >> connections to my server, with state TIME_WAIT. > > The TIME_WAIT state exists to drain "stray" packets; it is perfectly > normal. In particular, it does not indicate a problem with your code. > > Stare at a TCP state transition diagram until you understand it :). But there are no "stray" packets. Do I understand correctly, that the port remains open just in case some packets will arrive later? But this would mean, I could never restart a server which recently has been connected to. My server cannot be restarted in this case, because it cannot listen on an opened port, but e.g. apache does despite from its open TIME_WAIT connections. How have they done this? Thanks for your help so far! M@rtin .