Front-end - HTTP three-way handshake and four-way handshake

First of all, we need to know that in the process of sending and returning an http request between the client and the server, we need to create a TCP connection (TCP connection); because http only has requests and responses, there is no concept of connection; request and response They are all data packets, and they need to pass through a transmission channel. This transmission channel is a connection initiated by the client and received by the server through a TCP connection. This connection can always save the connection; the http request is sent on the basis of the connection; multiple http requests can be sent on the TCP connection (it is different in different http versions, in 1.0 this connection creates http at the same time Request, the TCP connection will be closed after the request is completed; in 1.1, the connection can be maintained through a certain declaration method, and after the first request is sent, the second request can be made; in version 2.0, the http request can be concurrent)

#illustrate

Image source: "Graphic HTTP"

Simple schematic:

Client – ​​send data packet with SYN/Seq flag – one handshake – server server – send data packet with SYN/ACK flag – two handshake – client client – ​​send with ACK flag Data packet – three-way handshake – server

The client initiates a request to create a connection packet (SYN/Seq) and sends it to the server. The server receives the client's request and returns a packet with the SYN/ACK flag, where ACK is the client sending Seq + 1, and Send a server-side Seq again. The client receives the data sent by the server, indicating that the connection is allowed, and sends data to the server again to confirm that the data can be received, and sends ACK equal to the Seq + 1 returned in the previous step, and the new data Seq; #Why three-way handshake

Simply put, it is to prevent the server from opening some useless connections; because the network connection is delayed (because it needs to be transmitted in various complicated ways); a three-way handshake is required to ensure that the client and the server remain connected and send The data was successful.

The first handshake: Client can't confirm anything; Server confirms that the other party's sending is normal, and it's receiving normally

The second handshake: Client confirms: its own sending and receiving are normal, the other party's sending and receiving are normal; Server confirms: the other party's sending is normal, and its own receiving is normal

The third handshake: Client confirms: its own sending and receiving are normal, and the other party's sending and receiving are normal; Server confirms: its own sending and receiving are normal, and the other party's sending and receiving are normal

Therefore, the three-way handshake can confirm that the functions of dual transmission and reception are normal, and both are indispensable.

#four wave

#TCP close connection process

The client sends a FIN packet to the server, indicating that the client actively wants to close the connection, and then enters the FIN_WAIT_1 state, waiting for the server to return an ACK packet. After that, Client can no longer send data to Server, but can read data. After receiving the FIN packet, the Server sends an ACK packet to the Client, and then enters the CLOSE_WAIT state. After that, the Server can no longer read data, but can continue to send data to the Client. Client enters FIN_WAIT_2 state after receiving ACK packet returned by Server, waiting for Server to send FIN packet. After the server finishes sending the data, it sends the FIN packet to the client, and then enters the LAST_ACK state, waiting for the client to return the ACK packet. After that, the server can neither read nor send data. After receiving the FIN packet, the Client sends an ACK packet to the Server, then enters the TIME_WAIT state, waits for a long enough time (2MSL) to ensure that the Server receives the ACK packet, and finally returns to the CLOSED state to release network resources. After receiving the ACK packet returned by the Client, the Server returns to the CLOSED state to release network resources.

#why wave four times

TCP is a full-duplex channel. What is full-duplex is that the client and the server establish two channels. Channel 1: the output of the client is connected to the input of the server; channel 2: the input of the client is connected to the output of the server. Both channels can work at the same time: while the client sends a signal to the server, the server can also send a signal to the client. So when the dual channel is turned off, it is like this:

Client: I'm closing the input channel. Server: OK, you can close it, and I will also close this channel here.

Server: I'm going to close the input channel too. Client: Okay, close it, I will close this channel too.

 

Guess you like

Origin blog.csdn.net/qq_40999917/article/details/128943325