CLOSE
Updated on 12 Aug, 20255 mins read 3 views

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:

StepDirectionFlags SentMeaning
1Client → ServerFINClient done sending data
2Server → ClientACKServer acknowledges client’s close request
3Server → ClientFINServer done sending data
4Client → ServerACKClient acknowledges server’s close request

 

Leave a comment

Your email address will not be published. Required fields are marked *