Subj : Re: Closing tcp socket connections To : comp.programming From : Alex Fraser Date : Mon Aug 15 2005 06:41 pm "Martin Hierholzer" wrote in message news:ddq60g$ihf$1@sagnix.uni-muenster.de... > 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!) Ah, OK. > >> 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. Normally, no - the underlying cause is packet loss or excessive delay, which is generally pretty rare. But the possibility must be handled, and that is what TIME_WAIT is for. [snip] > 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? Probably setsockopt(...SO_REUSEADDR...). Alex .