Moment For Technology

Why is TCP a three-way handshake instead of a two-way or four-way handshake?

Posted on Dec. 3, 2022, 9:13 a.m. by Maia Stone
Category: The back-end Tag: linux The back-end

This is the fourth day of my participation in the August More text Challenge. For details, see: August More Text Challenge

1 Three handshakes

2 Two Handshakes (Situation 1)

2 Two Handshakes (Situation 2)

OK, let's answer this question in earnest. To understand this question, you need to understand how TCP guarantees reliable transport.

PS: In TCP, the end that initiates the request is called the client, and the end that initiates the passive connection is called the server. Both the client and the server can send and receive data after the TCP connection is established.

Initially, both the server and client are in the CLOSED state. Before communication can begin, each party must create its own transmission Control block (TCB). After the TCB is created, the server enters the LISTEN state and is ready to receive connection requests from clients.

The first handshake

The client sends a connection request packet segment to the server. In the header of the packet, SYN=1, ACK=0, and seq=x. After the request is SENT, the client enters the SYN-sent state.

  • PS1: SYN=1 and ACK=0 indicates that the packet is a connection request packet.
  • PS2: x is the initial serial number of the byte stream for this TCP communication. TCP specifies that a segment with SYN=1 cannot have a data segment, but consumes a sequence number.

Second handshake

When a server receives a connection request segment and agrees to connect, it sends a response: SYN=1, ACK=1, SEQ =y, ACK= x+1. After the reply is sent, the syn-RCVD state is entered.

  • PS1: SYN=1, ACK=1 indicates that the packet is a response packet with connection approval.
  • PS2: seq=y indicates the initial sequence number of the byte stream sent when the server acts as the sender.
  • PS3: ACK =x+1 indicates that the server expects the next datagram to send bytes with serial number starting from x+1.

The third handshake

After receiving the connection consent reply, the client also sends an acknowledgement packet to the server, indicating that the connection consent reply sent by the server has been successfully received. The header of the packet is ACK=1, seq=x+1, ACK= y+1. After sending this segment, the client enters the ESTABLISHED state. After receiving this response, the server enters the ESTABLISHED state. The connection is ESTABLISHED.

3 Why do you need three handshakes instead of two to establish a connection?

Prevents invalid connection request packet segments from being received by the server, resulting in errors.

PS: Invalid connection request: If the connection request sent by the client to the server is lost, the client will send the connection request again after waiting for the reply timeout. In this case, the last connection request is invalid.

If you only need two handshakes to establish a connection, the client is not much changed and still needs to get a reply from the server before entering the ESTABLISHED state, whereas the server enters the ESTABLISHED state after receiving the connection request. In this case, if the network is congested and the connection request sent by the client cannot reach the server for a long time, the client resends the request after timeout. If the server receives and acknowledges the reply correctly, the two parties start the communication and release the connection after the communication ends. At this point, if the failed connection request reaches the server, since there are only two handshakes, the server will enter the ESTABLISHED state upon receipt of the request, waiting to send data or actively sending data. However, the client has already entered the CLOSED state, and the server will wait forever, which wastes the connection resources of the server.

4 TCP waves four times

The release of a TCP connection takes a total of four steps, hence the term "four waves."

As we know, TCP connections are bidirectional, so of the four waves, the first two waves are used to disconnect connections in one direction, and the second two waves are used to disconnect connections in the other direction.

4.1 First wave

If user A considers the data transmission complete, it needs to send A connection release request to user B. The request contains only the packet header, and the main parameters in the header are FIN=1 and SEq = U. Then, A enters the FIN-wait-1 state.

  • PS1: FIN=1 indicates that the packet is a connection release request.
  • PS2: seq= U, u-1 is the number of the last byte sent from A to B.

4.2 Second wave

When B receives the connection release request, it notifies the corresponding application that the connection from A to B has been released. In this case, B enters the close-wait state and sends A response to release the connection. The packet header contains ACK=1, SEq = V, ACK= U +1.

  • PS1: ACK=1: The ACK value of all datagrams except the TCP connection request packet segment is 1, indicating the response.
  • PS2: seq= V, where v-1 is the number of the last byte sent by B to A.
  • PS3: ACK =u+1 indicates that you want to receive a segment starting at the u+1 byte and that you have successfully received the first U bytes.

After receiving the response, user A enters the FIN-wait-2 state and waits for user B to send A connection release request.

After the second wave, the connection between A and B is released. B will not receive any more data, and A will not send any more data. However, the connection between B and A still exists, and B can continue to send data to A.

4.3 Third wave

After sending all data to A, B sends A connection release request to A. The request header is FIN=1, ACK=1, SEq =w, ACK= U +1. B enters the last-ACK state.

4.4 Wave for the fourth time

After receiving the release request, user A sends A confirmation reply to user B. In this case, user A enters the time-wait state. This state will last for 2MSL. If there is no request from B for resend during this period, the TCB will be entered into the CLOSED state and the TCB will be revoked. After RECEIVING the acknowledgement, B will enter the CLOSED state and cancel TCB.

5 Why does USER A enter the time-wait state and then enter the CLOSED state after 2MSL

In order to ensure that B can receive A's confirmation reply. If A enters the CLOSED state directly after sending the acknowledgement reply, then if the reply is lost, B will resend the connection release request after the timeout. However, A has been CLOSED and will not respond. Therefore, B can never be CLOSED normally.

Search
About
mo4tech.com (Moment For Technology) is a global community with thousands techies from across the global hang out!Passionate technologists, be it gadget freaks, tech enthusiasts, coders, technopreneurs, or CIOs, you would find them all here.