Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why tcp connect termination need 4-way-handshake?

Tags:

tcp

When connection sets up, there is:

Client ------SYN-----> Server

Client <---ACK/SYN---- Server ----①

Client ------ACK-----> Server

and when termination comes, there is:

Client ------FIN-----> Server

Client <-----ACK------ Server ----②

Client <-----FIN------ Server ----③

Client ------ACK-----> Server

my question is why ② and ③ can not set in the same package like ① which is ACK and SYN set in one package ???

like image 391
touchstone Avatar asked Sep 14 '17 07:09

touchstone


People also ask

Why TCP connection termination is 4 way handshake?

In connection Termination : it takes four segments to terminate a connection since a FIN and an ACK are required in each direction. (3) means that sometime later the application that received the end-of-file will close its socket. This causes its TCP to send a FIN.

How is TCP connection terminated 4 way?

TCP Termination (A 4-way handshake) Any device establishes a connection before proceeding with the termination. TCP requires 3-way handshake to establish a connection between the client and server before sending the data. Similarly, to terminate or stop the data transmission, it requires a 4-way handshake.

Can we terminate TCP connection with 3-way handshake technique?

It is also possible to terminate the connection by a 3-way handshake, when host A sends a FIN and host B replies with a FIN & ACK (merely combines 2 steps into one) and host A replies with an ACK. This is perhaps the most common method.

How does TCP connection terminate?

The common way of terminating a TCP connection is by using the TCP header's FIN flag. This mechanism allows each host to release its own side of the connection individually. Suppose that the client application decides it wants to close the connection. (Note that the server could also choose to close the connection).


1 Answers

After googling a lot, I recognized that the four-way is actually two pairs of two-way handshakes.

If termination is a REAL four-way actions, the 2 and 3 indeed can be set 1 at the same packet.

But this a two-phase work: the first phase (i.e. the first two-way handshake) is :

Client ------FIN-----> Server  Client <-----ACK------ Server 

At this moment the client has been in FIN_WAIT_2 state waiting for a FIN from Server. As a bidirectional and full-duplex protocol, at present one direction has break down, no more data would be sent, but receiving still work, client has to wait for the other "half-duplex" to be terminated.

While the FIN from the Server was sent to Client, then Client response a ACK to terminate the connection.

Concluding note: the 2 and 3 can not merge into one package, because they belong to different states. But, if server has no more data or no data at all to be sent when received the FIN from client, it's ok to merge 2 and 3 in one package.

References:

  1. http://www.tcpipguide.com/free/t_TCPConnectionTermination-2.htm
  2. http://www.tcpipguide.com/free/t_TCPConnectionEstablishmentProcessTheThreeWayHandsh-3.htm
  3. http://www.tcpipguide.com/free/t_TCPOperationalOverviewandtheTCPFiniteStateMachineF-2.htm
like image 150
touchstone Avatar answered Sep 22 '22 05:09

touchstone