Bird
Raised Fist0
Computer Networksknowledge~6 mins

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

Choose your learning style10 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
FIN → A TCP flag indicating a device wants to finish sending data and close the connection.
ACK → A TCP flag used to acknowledge receipt of a message.
Four-way handshake → The process of closing a TCP connection using four messages: FIN, ACK, FIN, ACK.
Half-close → A 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.

Practice

(1/5)
1. What is the main purpose of the TCP four-way handshake during connection termination?
easy
A. To allow both sides to close their sending channels independently
B. To establish a new connection between two hosts
C. To encrypt data before transmission
D. To synchronize sequence numbers at the start of communication

Solution

  1. Step 1: Understand TCP connection termination

    TCP uses a four-way handshake to close connections, allowing each side to finish sending data independently.
  2. Step 2: Identify the purpose of the handshake

    This process ensures no data is lost and both sides agree to close the connection properly.
  3. Final Answer:

    To allow both sides to close their sending channels independently -> Option A
  4. Quick Check:

    Four-way handshake = independent close [OK]
Hint: Four steps mean both sides close separately [OK]
Common Mistakes:
  • Confusing termination with connection setup
  • Thinking handshake encrypts data
  • Assuming simultaneous close in TCP
2. Which of the following is the correct sequence of TCP flags used in the four-way handshake for connection termination?
easy
A. FIN, SYN, ACK, FIN
B. SYN, ACK, FIN, RST
C. ACK, SYN, FIN, ACK
D. FIN, ACK, FIN, ACK

Solution

  1. Step 1: Recall TCP termination flags

    The four-way handshake uses FIN and ACK flags to close the connection properly.
  2. Step 2: Identify correct flag order

    The sequence is FIN from one side, ACK from the other, then FIN from the second side, and finally ACK from the first side.
  3. Final Answer:

    FIN, ACK, FIN, ACK -> Option D
  4. Quick Check:

    Termination = FIN then ACK pairs [OK]
Hint: Termination uses FIN and ACK flags in pairs [OK]
Common Mistakes:
  • Including SYN or RST flags in termination
  • Mixing order of flags
  • Assuming simultaneous FIN flags
3. Consider this sequence of TCP flags exchanged between Host A and Host B during connection termination:
A sends FIN
B replies ACK
B sends FIN
A replies ACK
What is the state of the connection after Host A sends the final ACK?
medium
A. Connection is fully closed
B. Connection is half-closed, waiting for more data
C. Connection is still established
D. Connection is reset abruptly

Solution

  1. Step 1: Analyze the four-way handshake steps

    Host A sends FIN, B acknowledges, B sends FIN, and A acknowledges. This completes the four steps.
  2. Step 2: Understand connection state after final ACK

    After the last ACK, both sides have agreed to close, so the connection is fully closed.
  3. Final Answer:

    Connection is fully closed -> Option A
  4. Quick Check:

    Final ACK means full close [OK]
Hint: Final ACK completes termination [OK]
Common Mistakes:
  • Thinking connection stays half-open after final ACK
  • Confusing reset with normal close
  • Assuming data can still be sent
4. A TCP connection termination sequence is observed as:
Host A sends FIN
Host B sends FIN
Host A sends ACK
Which step is missing or incorrect in this sequence?
medium
A. Host B should send RST instead of FIN
B. Host A should send SYN before FIN
C. Host B should send ACK before sending FIN
D. Host A should send FIN twice

Solution

  1. Step 1: Review correct four-way handshake order

    After Host A sends FIN, Host B must acknowledge (ACK) before sending its own FIN.
  2. Step 2: Identify missing ACK

    The sequence shows Host B sending FIN without ACKing Host A's FIN first, which is incorrect.
  3. Final Answer:

    Host B should send ACK before sending FIN -> Option C
  4. Quick Check:

    ACK always precedes FIN from other side [OK]
Hint: ACK must come before FIN from other side [OK]
Common Mistakes:
  • Skipping ACK step
  • Confusing SYN with FIN
  • Thinking RST is normal close
5. During a TCP connection termination, if Host A sends a FIN and immediately closes its socket without waiting for the ACK from Host B, what is the likely outcome?
hard
A. Host B will retransmit FIN until it receives ACK, causing delays
B. The connection will close cleanly without issues
C. Host A will receive data after closing the socket
D. Host B will reset the connection immediately

Solution

  1. Step 1: Understand proper termination behavior

    In TCP, after sending FIN, the application closes the socket, but the TCP stack manages states like FIN_WAIT_1, FIN_WAIT_2, and TIME_WAIT independently.
  2. Step 2: Analyze consequence of immediate close

    The stack processes incoming ACK and FIN from Host B, sending responses as needed, so the connection closes cleanly without issues.
  3. Final Answer:

    The connection will close cleanly without issues -> Option B
  4. Quick Check:

    Stack handles states after socket close [OK]
Hint: TCP stack handles states post-socket close [OK]
Common Mistakes:
  • Thinking immediate close causes reset by peer
  • Expecting retransmissions without ACK loss
  • Confusing socket close with immediate connection abort
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard