Closing A Connection
TCP opens with a three way handshake, but it closes with a separate four way exchange so each direction can stop independently. This is called a graceful close.
The FIN Exchange
- The side that is done sends a FIN segment, signaling it has no more data.
- The peer acknowledges that FIN but may keep sending its own remaining data.
- When the peer finishes, it sends its own FIN.
- The first side acknowledges, and both directions are now closed.
Because each direction closes on its own, a connection can be half closed, still receiving while no longer sending.
Why Time Wait Exists
After sending the final acknowledgment, the active closer enters TIME WAIT for roughly twice the maximum segment lifetime. This wait serves two goals:
- It lets a lost final acknowledgment be retransmitted if the peer resends its FIN.
- It ensures stray old segments drain from the network before the same address and port pair is reused.
The Practical Cost
A busy server that actively closes many connections can accumulate thousands of sockets stuck in TIME WAIT, consuming port and memory resources. Connection reuse and letting clients close first both ease this pressure.
Key idea
TCP closes each direction with a FIN and ACK, and the active closer waits in TIME WAIT so lost acknowledgments and stray old segments cannot corrupt a reused address pair.