Just like TCP connections start with a three-way handshake, they end with a controlled shutdown.
This ensures both sides agree that the communication is finished and all remaining data has been delivered.
Closing a TCP connection typically uses a four-step process known as the Four-Way Handshake.
The Four-Step Connection Teardown
A TCP connection is full-duplex – data can flow in both directions independently.
This means each side must close its half of the connection separately.
Here' show it works:
Step 1: FIN from the First Side
- One side (say, the client) decides to close its sending side of the connection.
- It sends a TCP segment with the FIN flag set.
- This means: “I have no more data to send.”
Step 2: ACK from the Other Side
- The receiver acknowledges the FIN by sending back an ACK.
- This means: “I understand you are done sending, but I might still have data to send.”
Step 3: FIN from the Second Side
- When the second side is also ready to close its sending side, it sends its own FIN.
- This means: “I am done sending too.”
Step 4: ACK from the First Side
- The original closer acknowledges the second FIN by sending back an ACK.
- Now, both directions are closed.
Example Sequence:
Step | Direction | Flags Sent | Meaning |
---|---|---|---|
1 | Client → Server | FIN | Client done sending data |
2 | Server → Client | ACK | Server acknowledges client’s close request |
3 | Server → Client | FIN | Server done sending data |
4 | Client → Server | ACK | Client acknowledges server’s close request |
Leave a comment
Your email address will not be published. Required fields are marked *