The http protocol layering of the TCP/IP reference model, three-way handshake, four-way wave

insert image description here
insert image description here
insert image description here

The process of shaking hands three times and waving four times:

Three-way handshake:

The process of TCP establishing a connection is called the 3-way handshake.
(1) For the first handshake,
PC1 uses a random port number to send a connection request to port 80 of PC2. The typical sign of this process is that the SYN control bit is 1, and the other five bits are 0.
(2) The second handshake
This handshake is actually completed in two steps.
First, PC2 receives the request from PC1 and returns a confirmation message to PC1.
Moreover, PC2 also sends a connection establishment request to PC1.
(3) After the third handshake,
PC1 receives a reply from PC2, and also sends a confirmation message to PC1.

Wave four times:

The process of TCP disconnection is divided into 4 steps, which we call four waves.
(1) The server sends FIN to the client, and the ACK bit is 1 to get the TCP segment.
(2) The client returns the TCP segment with ACK position 1 to the server.
(3) The client sends FIN to the server, and the ACK bit is 1 to get the TCP segment.
(4) The server returns the TCP segment with ACK position 1 to the client.
In the process of TCP disconnection, there is a concept of half-close. The TCP end can stop sending data, but can still receive data, which is called half-closed:
(1) The client sends FIN, which half-closes the link. The server sends an ACK to accept the half close.
(2) The server continues to send data, while the client only sends ACK confirmation without sending any data.
(3) When all the data transmission of the server is completed, the FIN message segment is sent, and the client sends the ACK message segment, thus closing the TCP connection.

What is the essence of the three-way handshake and four-way wave?

The essence of the three-way handshake is to confirm the ability of the communicating parties to send and receive data.
The purpose of the four waves is to close a connection.

Why is it 3 times when TCP is connected? 2 times can't do it?

Because it is necessary to consider the problem of packet loss during connection , if there are only two handshakes, if the confirmation segment sent by the server to the client is lost during the second handshake, the server is ready to send and receive data at this time (it can be understood that the server has already The connection is successful), but the client has not received the confirmation message from the server, so the client does not know whether the server is ready (it can be understood that the client is not connected successfully), in this case the client will not give When the server sends data, it also ignores the data sent by the server.
If it is a three-way handshake, even if there is a packet loss, there will be no problem. For example, if the confirmation ack message sent by the client in the third handshake is lost, if the server does not receive the confirmation ack message within a period of time, it will start again. The second handshake means that the server will resend the SYN segment, and the client will send an ack message to the server again after receiving the resent segment.
insert image description here

Why is it 3 times when TCP is connected, but 4 times when it is closed?

Because TCP can only be disconnected when neither the client nor the server has data to send. When the client sends a FIN message, it can only guarantee that the client has no data to send, and the server does not know whether there is any data to send to the client. After receiving the FIN message from the client, the server can only reply to the client with a confirmation message to tell the client that the server has received your FIN message, but my server still has some data that has not been sent. The server can only send the FIN message to the client after the data has been sent (so the confirmation message and the FIN message cannot be sent to the client at one time, because there is an extra time here).

Why does the client have to wait for 2MSL to release the TCP connection after sending the confirmation message for the fourth wave? That is, why does the client have to wait for 2MSL in the TIME-WAIT phase?

MSL refers to the Maximum Segment Lifetime: the maximum life cycle of a TCP packet during transmission.
2MSL is the maximum duration that the FIN message sent by the server and the ACK confirmation message sent by the client can remain valid.
Here we also need to consider the problem of packet loss. If the message for the fourth wave is lost, the server will resend the message for the third wave if it does not receive the confirmation ack message. It is 2MSL, so it takes so long to confirm that the server has indeed received it.

What if the connection is established, but the client suddenly fails?

TCP has a keep-alive timer. If the client fails, the server cannot wait forever, wasting resources in vain. The server will reset the timer every time it receives a request from the client. The time is usually set to 2 hours. If it has not received any data from the client within two hours, the server will send a probe segment. After that, every 75 sent every second. If there is still no response after sending 10 detection messages in a row, the server thinks that the client has failed, and then closes the connection.

Summarize

A calls B on the phone
A: Hello, is this B? 1 handshake
B: Hello, I am B, who are you? Second handshake
A: I am A, I have something to tell you Three handshake
A: xxxx content

The process of browser access

1. The domain name is resolved into an IP address. If there is no cache, it will request the DNS server;
2. Make a TCP connection with the target server (three-way handshake)
3. Send and receive data (browser and server access through protocols {http, FTP, etc.}) Process
4, browser and server port TCP connection (wave four times)

insert image description here
insert image description here

TCP/UDP comparison

Both work at the host-to-host layer (transport layer)

TCP: Transmission Control Protocol, which provides reliable services and is a connection-oriented network protocol.
Use TCP applications: Web browsers; emails; file transfer programs

UDP: User Message Protocol, providing best-effort service, belongs to connectionless network protocol
Application of UDP: Domain Name System (DNS); video streaming

Guess you like

Origin blog.csdn.net/u013400314/article/details/131707799