Karan Singh
Karan Singh

Reputation: 1164

Why does TCP connection termination require 4 steps?

So basically, I was asked this question for an interview. The question was:

Why does TCP connection termination require 4 steps?

From this I know that, FIN and ACK need to be sent by both sides to complete termination, but another follow up question I couldn't answer was that why does the server have to send 2 messages ( each with only the FIN and ACK bit set respectively).

Why can't it be done together?

Upvotes: 0

Views: 1249

Answers (2)

Pomato
Pomato

Reputation: 11

Close is an operation meaning "I have no more data to send." refer to RFC793.

         TCP A                                                TCP B
  1.     ESTABLISHED                                          ESTABLISHED

  2.     (Close)
         FIN-WAIT-1  --> <SEQ=100><ACK=300><CTL=FIN,ACK>  --> CLOSE-WAIT

  3.     FIN-WAIT-2  <-- <SEQ=300><ACK=101><CTL=ACK>      <-- CLOSE-WAIT

  4.                                                          (Close)
         TIME-WAIT   <-- <SEQ=300><ACK=101><CTL=FIN,ACK>  <-- LAST-ACK

  5.     TIME-WAIT   --> <SEQ=101><ACK=301><CTL=ACK>      --> CLOSED

  6.     (2 MSL)
         CLOSED

So you can see the client already had no data to send when step2, but the server had not the data to send until step4. The server needs another FIN to tell the client "I have no data to send". That's why it can't work just like TCP connection by 3 steps.

Upvotes: 0

Steffen Ullrich
Steffen Ullrich

Reputation: 123380

Why can't it be done together?

It can be done together and this is actually the common case in practice, i.e.

-> FIN
<- FIN, ACK to FIN
-> ACK to FIN

But it can also be done separately especially if the peer does not shutdown immediately but still sends data:

-> FIN
<- DATA, ACK to FIN
.... <- more DATA (+ again ACK to FIN)
<- FIN (+ again ACK to FIN)
-> ACK to FIN 

Upvotes: 2

Related Questions