0
0
Computer Networksknowledge~6 mins

TCP connection termination (four-way handshake) in Computer Networks - Full Explanation

Choose your learning style9 modes available
Introduction
Imagine you are ending a phone call and want to make sure both sides hang up properly. In computer networks, closing a connection between two devices needs a careful process to avoid losing data or leaving the connection half-open.
Explanation
FIN from Initiator
The device that wants to close the connection sends a FIN (finish) message to signal it has no more data to send. This tells the other side that it can stop expecting data from the initiator.
The initiator starts closing by sending a FIN to say it is done sending data.
ACK from Receiver
The receiving device acknowledges the FIN by sending an ACK (acknowledgment) message back. This confirms it received the close request and knows the initiator is done sending data.
The receiver confirms the close request by replying with an ACK.
FIN from Receiver
When the receiver is ready to close its side, it sends its own FIN message. This means it also has no more data to send and wants to end the connection from its side.
The receiver signals it is done by sending its own FIN.
ACK from Initiator
Finally, the initiator sends an ACK to confirm it received the receiver's FIN. After this, both sides know the connection is fully closed and can release resources.
The initiator confirms the receiver's FIN with an ACK, completing the termination.
Real World Analogy

Imagine two friends finishing a phone call. One says, 'I'm done talking,' and the other replies, 'Okay, I heard you.' Then the second friend says, 'I'm done too,' and the first friend replies, 'Got it, bye!' This way, both know the call ended properly.

FIN from Initiator → First friend saying 'I'm done talking'
ACK from Receiver → Second friend replying 'Okay, I heard you'
FIN from Receiver → Second friend saying 'I'm done too'
ACK from Initiator → First friend replying 'Got it, bye!' to confirm
Diagram
Diagram
Initiator          Receiver
   |                   |
   |---- FIN ---------> |
   |                   |
   |<--- ACK ---------- |
   |                   |
   |<--- FIN ---------- |
   |                   |
   |---- ACK ---------> |
   |                   |
Sequence of messages exchanged during TCP connection termination showing the four steps.
Key Facts
FINA TCP flag indicating a device wants to finish sending data and close the connection.
ACKA TCP flag used to acknowledge receipt of a message.
Four-way handshakeThe process of closing a TCP connection using four messages: FIN, ACK, FIN, ACK.
Half-closeA state where one side has finished sending data but still receives data from the other side.
Common Confusions
Believing TCP connection closes with just one FIN message.
Believing TCP connection closes with just one FIN message. TCP requires a four-step handshake with two FIN and two ACK messages to ensure both sides close properly.
Thinking ACK means the connection is fully closed.
Thinking ACK means the connection is fully closed. An ACK only confirms receipt of a FIN; the connection closes only after both sides exchange FIN and ACK.
Summary
TCP connection termination uses a four-step handshake to close both sides cleanly.
Each side sends a FIN to signal it is done sending data and an ACK to confirm receipt.
This process avoids data loss and ensures both devices know the connection is closed.