TCP UDP protocol learning

First of all, the network is divided into the following layers, and TCP/UDP both work at the transport layer.

It is mainly used to transfer data between programs. The data can be text files, videos, pictures, etc. For TCP/UDP, they are all a bunch of binary numbers, and there is not much difference. The specific difference is a connection-based (TCP) , a connectionless (UDP) based.

 

If the communication between people is compared to the communication between processes, there are two ways, one is to write a letter and the other is to make a phone call. There is no response to writing a letter, including whether the recipient has received it and whether the address is correct. All this is unknown; but making a call can determine whether the other party is connected and whether the phone number is correct. All this is known. TCP and UDP are similar to the above two situations, see the diagram below for details. The picture comes from the video screenshot of the UP master of station B.

The reason why TCP can ensure the correct completion of the above process is mainly in the following three aspects: three-way handshake, transmission confirmation, and four-way wave .

Three-way handshake:

The three-way handshake is a connection-based process. When sending data, the client first releases a packet of request data ( SYN packet ), and if the server agrees to the connection, it replies with a packet ( SYN+ACK packet ), and the client replies with a packet ( ACK packet ), the connection is established.

 Because the above process sends three packets, it is called a three-way handshake.

Why not a two-way handshake but a three-way handshake?

Prevent invalid messages from being transmitted to the server suddenly and cause errors. For example, the client first sends a SYN packet to the server. Due to network congestion, the server does not receive the SYN packet. At this time, the client sends another SYN packet, and the server receives and replies the SYN+ACK packet; at this time The last SYN packet was sent to the server again due to the recovery of the network. At this time, the server thought it was two connections, but the client still thought it was a connection established, resulting in conflicts and inconsistencies between the two parties. If it is in the case of a three-way handshake, the server cannot receive the final ACK packet, and naturally it will not consider the connection to be successful. So the three-way handshake is essentially to solve the problem of unreliable network channels, in order to be able to establish reliable connections on unreliable channels .

sending method:

TCP is to establish a reliable connection on an unreliable network channel. It mainly faces the following two problems, one is the problem of packet loss, and the other is the problem of disorder . In order to solve this problem, the TCP protocol establishes a sending buffer for each connection. Area , as shown in the figure below: the first sequence number after the connection is established is 0, and the following will be +1 each time , and each time it is sent, a part of the data is taken from the buffer to form a sending message, and the sequence is attached in the TCP protocol header number and length , the sequence number and length of the data contained in the confirmation message ACK, that is, the starting sequence number for the next connection transmission. Such a transmission method can confirm that the data sent by the sender has been received, so that multiple packets of data can be sent continuously, and the receiver only needs to reply with an ACK. The sending end can divide the data to be sent into a series of fragments ( cutting and sending ), and send it to the peer end, and the peer end can reassemble the complete data according to the sequence number and length. If some data packets are lost, the receiving end can ask the sending end to retransmit. For example, if the data of 100 bytes of 100-199 is lost, the receiving end sends an ACK=100 message to the sending end. Retransmit the data of this packet, and the receiving end completes it.

Note that the above process does not distinguish between the client and the server, because TCP is full-duplex , and the above mechanism is used for both ends.

Wave four times:

Both the server and the client can actively initiate a connection close request.

For example, the client actively sends a connection close request, the process is shown in the figure, and the specific explanation is as follows:

  • The client sends a FIN packet, and then enters the waiting 1 state (WAIT-1), waiting for the ACK packet sent by the server.
  • After receiving the FIN packet, the server enters the close waiting state (CLOSE-WAIT) after sending the ACK packet, and the client enters the termination waiting 2 state. The server can still send unsent data at this time, and the client can also receive data.
  • After the server sends the data, it sends a FIN packet to the client and enters the final confirmation state.
  • After receiving it, the client replies with the ACK packet, enters the timeout waiting state, and closes the connection after the timeout. The server closes the connection immediately after receiving the ACK packet.

 

Why does the client need to wait for the timeout?

In order to ensure that the other party has received the ACK packet, if the client releases the connection immediately after sending the ACK packet, but due to network problems, the server does not receive the ACK packet, and the server will remain in the final confirmation state. If the server does not receive the ACK packet, the client waits for a period of time, then the server will resend the FIN packet, and the client will resend the ACK packet in response to the FIN packet, and refresh the timeout period. This mechanism, like the three-way handshake, is for reliable disconnection confirmation during unreliable network connections.

UDP

The UDP connection is based on non-connection. Sending data is simply encapsulating the data packet, and then sending it out from the network card. There is no state connection between the data packets. The speed is faster, but there may be packet loss problems.

The main difference between TCP and UDP 

 

TCP is suitable for scenarios that require high data stability, while UDP is suitable for scenarios that require high speed and low data integrity.

Guess you like

Origin blog.csdn.net/m0_46259216/article/details/127624748