CodePark/ questions / TCP connections in depth
START PRACTICINGPRACTICE
Backend question · OS, networking & Linux › TCP connections in depth

Your server calls close() on a connection first, and the client closes its side afterwards. Once the close completes, which side's socket is left in TIME_WAIT?

The server's, because it closed first and is the active closer

The server's socket is left in TIME_WAIT. TIME_WAIT always stays on the side that sends the first FIN (the active closer). Here, that is the server.

The client does a
passive close. Its socket moves through CLOSE_WAIT and LAST_ACK. When the server acknowledges the client's own FIN, the client's socket is gone. So "The client's, because it sent the last FIN" is wrong: sending the last FIN does not create TIME_WAIT.

This is why servers that close every connection themselves collect many TIME_WAIT sockets.