Computer Network Learning 08 (TCP three-way handshake and four-way wave)

1. Establish a connection—TCP last handshake

insert image description here
Establishing a TCP connection requires a "three-way handshake", both of which are indispensable:

  • One handshake: the client sends a data packet with the SYN (SEQ=x) flag -> server, and then the client enters SYN_SENDthe state, waiting for the confirmation of the server;
  • Second handshake: the server sends a packet with the SYN+ACK (SEQ=y, ACK=x+1) flag –> the client, and then the server enters the SYN_RECVstate
  • Three-way handshake: The client sends a packet with the ACK (ACK=y+1) flag –> the server, and then both the client and the server enter the state to ESTABLISHEDcomplete the TCP three-way handshake.

After the 3-way handshake is established, the client and server can transmit data!

2. Why the three-way handshake?

The purpose of the three-way handshake is to establish a reliable communication channel. When it comes to communication, it is simply the sending and receiving of data. The main purpose of the three-way handshake is to confirm that the sending and receiving between the two parties is normal.

1. The first handshake: Client can’t confirm anything; the server confirms that the other party’s transmission is normal, and its own reception is
normal . The other party sends and receives normally. 3. The third handshake: Client confirms: the client sends and receives normally, and the other party sends and receives normally; Server confirms: it sends and receives normally, and the other party sends and receives normally

The three-way handshake can confirm that the sending and receiving functions of both parties are normal, and both are indispensable.

3. The 2nd handshake sent back ACK, why did it send back SYN?

The server sends back the ACK sent by the sender to tell the client: "The information I received is indeed the signal you sent", which indicates that the communication from the client to the server is normal. The return SYN is to establish and confirm the communication from the server to the client.

SYNThe synchronization sequence number (Synchronize Sequence Numbers)is a handshake signal used by TCP/IP to establish a connection. When a normal TCP network connection is established between the client and the server, the client first sends a SYN message, the server responds with a SYN-ACK to indicate receipt of the message, and finally the client responds with an ACK (Acknowledgment) message. In this way, a reliable TCP connection can be established between the client computer and the server, and data can be transmitted between the client computer and the server.

4. Disconnect - TCP waved four times

insert image description here

Disconnecting a TCP connection requires "four waves", both of which are indispensable:

  • The first wave: the client sends a data packet with a FIN (SEQ=X) flag -> server, which is used to close the data transmission from the client to the server. Then, the client enters FIN-WAIT-1the state.
  • The second wave: the server receives this FIN (SEQ=X) flag packet, it sends an ACK (SEQ=X+1) flag packet -> client. Then, at this time, the server enters CLOSE-WAITthe state, and the client enters FIN-WAIT-2the state.
  • The third wave: the server closes the connection with the client and sends a FIN (SEQ=y) flag packet -> the client requests to close the connection, and then the server enters the state LAST-ACK.
  • The fourth wave: the client sends a data packet marked with ACK (SEQ=y+1) -> server and enters the TIME-WAITstate, and the server enters the state after receiving the data packet marked with ACK (SEQ=y+1) CLOSE. At this time, if the client 2MSLstill does not receive a reply after waiting, it proves that the server has been closed normally, and then the client can also close the connection.

As long as the four waves are not over, the client and server can continue to transmit data!

5. Why do you wave your hands four times?

TCP is full-duplex communication, which can transmit data in both directions. Either party can issue a notification of connection release after the data transmission is completed, and enter the half-closed state after the other party confirms. When the other party has no data to send again, a connection release notification is issued, and the TCP connection is completely closed after the other party confirms.

For example: A and B make a call, and the call is about to end.

1. The first wave: A says "I have nothing to say"
2. The second wave: B answers "I see", but B may still have something to say, A cannot ask B to follow his own Rhythm to end the call
3. Wave for the third time: So B may say something more about Barabara, and finally B says "I'm done"
4. Wave for the fourth time: A answers "Got it", so that the call is over.

6. Why can't the ACK and FIN sent by the server be combined into three waves?

Because when the server receives the request to disconnect from the client, there may still be some data that has not been sent. At this time, it will reply ACK first, indicating that it has received the request to disconnect. Wait until the data is sent before sending FIN to disconnect the data transmission from the server to the client.

7. What happens if the server's ACK is not delivered to the client when the second wave is made?

If the client does not receive the ACK confirmation, it will resend the FIN request.

8. Why does the client need to wait for 2*MSL (Maximum Segment Lifetime) for the fourth wave before entering the CLOSED state?

When waving for the fourth time, the ACK sent by the client to the server may be lost. If the server does not receive the ACK for some reason, the server will resend the FIN. If the client receives within 2*MSL When the FIN arrives, it will resend the ACK and wait for 2MSL again to prevent the Server from resending the FIN without receiving the ACK.

MSL(Maximum Segment Lifetime) :The maximum survival time of a fragment in the network, 2MSL is the maximum time required for a send and a reply. If the Client does not receive FIN again until 2MSL, then the Client infers that the ACK has been successfully received, and ends the TCP connection.

Guess you like

Origin blog.csdn.net/ldy007714/article/details/130411886