Subj : Closing tcp socket connections To : comp.programming From : Martin Hierholzer Date : Fri Aug 12 2005 02:25 pm Hi! I have a problem closing tcp network server sockets. I have written a (single threaded) server which is running on Linux (2.6.12-gentoo-r6) and a client which should run on Linux and Windows. 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. For testing stability, I send these small packets as fast as I can, so many packets arrive per second. To close the connection, the server makes calls to shutdown and close on the appropriate socket: i = send(ihConn, &iReply, sizeof(iReply), 0); if(i != sizeof(iReply)) { // do some error handling } i = shutdown(ihConn, SHUT_RDWR); if(i != 0) { // do some error handling } i = close(ihConn); if(i != 0) { // do some error handling } 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. I would expect only one connection at most, because the client makes only on connection at a time. After a while, the connections are gone. Of course, restarting the server while these connections are still there is not possible, because the server port cannot be opened. Sometimes the client stops with an error message, I think this is when the maximum number of ports is reached. Unfortunally these errors are very rare and I cannot reproduce them now, so I cannot tell you the exact messages (they are different on linux and windows!). What have I made wrong? How do I close the connections really? Greetings and thanks for your help! .