TCP connection establishment and disconnection process

In the process of establishing a TCP connection in CS mode, the client and server processes are as follows:

Client process: send request->receive server-side confirmation->send confirmation of server-side confirmation.

Server-side process: Receive the client's connection establishment request -> send confirmation -> receive the confirmation sent by the client.
Insert picture description here
1. At the beginning, both the client and the server are in the CLOSED state, and the server starts to monitor a certain port and enters the LISTEN state.

2. The client sends a connection request message, which contains SYN=1, ACK=0, and the initial sequence number x, and enters the SYN-SENT state.

3. After the server accepts the request message, it sends a confirmation message to the client, which contains SYN=1, ACK=1, confirmation number x+1, and its own initial serial number y, and then the server will enter the SYN-RCVD state.

4. After the client receives the confirmation message from the server, it will send a confirmation message to the server, indicating that it has received the confirmation message sent by the server. The message contains ACK=1, acknowledgment number y+1, and sequence number x+1. At the same time, the client will enter the ESTABLISHED state.

5. The server will enter the ESTABLISHED state after receiving the above message. At this time, the TCP connection between the client and the server is established.

Why is the TCP connection not a two-way handshake? That is why the client will finally send a confirmation message to the server.

1: Assuming that there are only two handshake, the client sends a connection request message A. However, due to network reasons, A did not reach the server in time. After that, the client sends a connection request message B and arrives at the server. Then the two parties establish a connection, transmit data, and finally disconnect. At this time, the client enters the CLOSED state, and the server enters the LISTEN state. Then the A message arrives at the server. Since the server is in the LISTEN state, the server enters the ESTABLISHED state after sending an acknowledgement message to the client and waits for the client to send data. But the client is in the CLOSED state at this time. After receiving the confirmation message from the server, the message will be discarded. The server has been waiting for the client to transmit data, which will cause a waste of secondary weapons.

2: TCP's reliable transmission requires both parties in communication to determine whether the data they send has been received. TCP relies on the sequence number and acknowledgment number in the TCP message. Therefore, when the TCP connection is established, both parties need to confirm the other party's initial sequence number. The third handshake is the client's confirmation of the server's initial serial number. Therefore, the third handshake is necessary, otherwise reliable transmission from the server to the client cannot be guaranteed.

Why is the TCP connection not a 4-way handshake?

The three-way handshake has been able to prove that the channel between the client and the server is available. Adding another handshake is nothing more than increasing the reliability of the channel, but no matter how many handshake times the channel is 100% reliable, there is no need to add another handshake.

The process of TCP disconnection
Insert picture description here
1. The client stops sending data and sends a connection release request message. The message contains FIN=1, seq=u, and enters the FIN-WAIT-1 state.

2. After receiving the message, the server returns a connection release confirmation message. Contains ACK=1, seq=v, ack=u+1, and enters the CLOSE-WAIT state. In this state, the server may also send data to the client.

3. After the client receives the confirmation message, it enters the FIN-WAIT-2 state, waits for the server to send the connection release request message, and receives the data that the server may send.

4. After the server has sent all the data, it will send a connection request release message. The message contains FIN=1, ACK=1, seq=w, ack=u+1,. After that, the server enters the LAST-WAIT state and waits for the connection release confirmation message sent by the client.

5. After the client receives the connection release message from the server, it returns, ACK=1, ack=w+1, and its own serial number is seq=u+1. At this time, the client enters TIME-WAIT ( Time waiting) state, the TCP connection is not released at this time, because it is uncertain whether there is data on the server side that has not reached the client side. Therefore, it must pass 2MSL (maximum message survival time) before entering the CLOSED state.
6. As long as the server receives the connection release confirmation message sent by the client, it immediately enters the CLOSED state.
The difference between HTTP1.0 and HTTP1.1 and HTTP2.0

Guess you like

Origin blog.csdn.net/qq_43248623/article/details/114685646