This is the 27th day of my participation in the August Genwen Challenge.More challenges in August

In the recent systematic study of network engineering related knowledge, found that many designs are very valuable for learning. Especially TCP network connection three wave and four wave related questions in the interview process has become the most will be asked, one of the most basic interview questions, through learning to establish a network connection process, self-summed up the source hot function and ideas, has a high learning value.

TCP three-way handshake

The three-way handshake process

  1. Client — sends a packet with the SYN flag — Server shakes the Client once and enters the syn_sent state (SYN=1,SEQ=x)
  2. Server — sends packets with SYN/ACK flags — Client twice shakes server into SYN_RCVD (during which the sender responds ACK =X+1 and sends SYN=1, SEQ= Y)
  3. Client — sends packet with ACK flag — Server enters Established state after three handshake (during the process, sender feedback ACK = Y +1 and sends sequence signal seq=z)

The reason for three handshakes

In the process of three handshakes, a reliable communication channel is established. At the same time, ensure that the client and server have the ability to send and receive data.

The possibility of only shaking hands twice

No. If you shake hands twice, you can only ensure that a single channel is normal. During a handshake, the sender sends its own serial number and performs the +1 operation on the serial number provided by the recipient. In this way, the order of sending and receiving in the process is completely in accordance with the timing. In the case of two handshakes, at most only the initial sequence number of the connection initiator can be confirmed, and the sequence number selected by the other party cannot be confirmed.

2 handshakes can cause problems

The most common is deadlocks. For example, user A sends A request to user B, and user B sends A request to establish A connection. If user B shakes hands twice, it thinks that data can be transmitted and is waiting for the data group to be transmitted. If B’s return signal is lost during this process, A is still waiting for A response. In this case, A will wait forever, and B will send the same data repeatedly because the packet times out. Cause a deadlock.