[Network] The difference between TCP and UDP

1. What is the TCP/IP protocol?

The TCP/IP protocol is not one protocol, but is composed of two protocols. The Chinese translation is: Transmission Layer Control Protocol/Internet Protocol, which is composed of the underlying IP protocol and TCP protocol.

1.1 IP protocol:

First of all, let's understand what the IP protocol is. The translation of IP protocol is Internet protocol.

The IP protocol can be understood in life as: when we transport goods, we all have to pack the goods into cartons or containers before transporting them. The same is true in the network transmission process. The IP protocol specifies the basic unit and format of data transmission. For example, compared to the transportation of goods, the IP protocol specifies the box size and packaging procedures when the goods are packaged. In addition, the IP protocol also defines the delivery method and routing of the data packet. Similarly, using the transportation of goods as an analogy, the IP protocol stipulates the mode and route of transportation of goods.

1.2 TCP protocol:

The important protocol of the transport layer, the IP protocol has stipulated the main content of data transmission, the transmission defined in the IP protocol is one-way, that is, whether the goods sent out have been received by the other party, we don't know this. Therefore, for some of the more important goods, we want to know whether the other party has actually received it and ask the other party to give us a response. Therefore, the TCP protocol provides rules and conventions for reliable object-oriented data streaming services. That is, in the TCP model, the other party sends you a data packet, and you have to send an acknowledgement data packet to the other party, through this mechanism to provide a reliable connection.

2.UDP

First of all, we must understand that TCP and UDP are two protocols of the transport layer.
UDP header: It
Insert picture description here
can be seen that UDP only has a port number, and the others are basically gone. Without these two port numbers, the data will not know which application to send to.

Features of
UDP : The process of UDP transmission is similar to sending a letter (as long as you know the address, you can send the letter directly):
No connection: You can directly transmit when you know the IP and port number of the opposite end, without establishing a connection;
Unreliable: No confirmation mechanism , There is no retransmission mechanism; if the segment cannot be sent to the other party due to a network failure, the UDP protocol layer will not return any error information to the application layer;
datagram-oriented: it cannot flexibly control the number and quantity of data read and written. In other words, the application layer will hand over the UDP packets as long as they are, and UDP will not be split or merged.

Based on these characteristics of UDP, it can be used to deal with some projects that are not so difficult, and even if it fails, it can be received.

UDP buffer:
UDP does not have a sending buffer in the true sense. Calling sendto will directly hand it over to the kernel, and the kernel will pass the datagram to the network layer protocol for subsequent transmission actions.
UDP has a receiving buffer, but this buffer cannot guarantee that the sequence of the received UDP datagrams is consistent with the sequence of the sent UDP. If the buffer is full, the UDP datagrams that arrive again will be discarded.

Application scenarios of UDP:

  • Live. Live broadcast has relatively high requirements for real-time performance. It would rather lose packets than freeze, so many live broadcast applications are implemented based on UDP.

  • Real-time gaming. It is also characterized by relatively high real-time requirements. In this case, the use of a custom and reliable UDP protocol and a custom retransmission strategy can minimize the delay caused by the network.

  • Internet of Things. The requirements for real-time performance are also particularly high.

UDP-based application layer protocol:

NFS: Network File System
TFTP: Simple File Transfer Protocol
DHCP: Dynamic Host Configuration Protocol
BOOTP: Boot Protocol (for diskless device boot)
DNS: Domain Name Resolution Protocol

3. TCP

TCP: Transmission Control Protocol, as the name implies, strictly controls the transmission of data.

3.1 TCP data format:

Insert picture description here
Source/destination port number : indicates which process the data comes from and to which process.
32-bit serial number/32-bit confirmation number : mainly to solve the problem of disorder.
4-bit TCP header length : Indicates how many 32-bit bits (how many 4 bytes are there)
6 flag bits : URG: Whether the emergency pointer is valid
SYN is to initiate a link, ACK is a reply, and RST is a renewal Connection, FIN is to end the connection.
16-bit checksum : the sender fills, CRC check, and the receiver fails the check, then the data is considered to be problematic. The check includes the TCP header and the TCP data part.
16-bit emergency pointer : identifies which part of the data is urgent data

3.2 For the understanding of TCP protocol, the following issues should also be paid attention to:

First of all, we must understand why the TCP protocol is so complicated? Because the TCP protocol must ensure a reliable connection and improve performance as much as possible.

Guarantee reliability:

  • Checksum
  • Serial number (arrival in order)
  • Confirm response
  • Retransmit after timeout
  • Connection management
  • flow control
  • Congestion management

Improve performance

  • Sliding window
  • Fast retransmission
  • Delayed response
  • Piggyback response

other

  • Timers (timeout retransmission timer, keep-alive timer, TIME_WAIT timer, etc.)

The first is the TCP connection problem, because at the transport layer, the client first needs to establish a connection with the server, which is the three-way handshake phase.

3.3 Three-way handshake

Insert picture description here

3.4 Waves four times:

Insert picture description here

3.5 Confirmation response mechanism

When sending: carry the confirmation serial number;
when confirming: carry the confirmation number.
Serial number + confirmation number
Insert picture description here

3.6 Timeout retransmission mechanism

Circumstances where the confirmation response is not received:
(1) The datagram is lost;

Insert picture description here
(2) The ACK acknowledgement datagram is lost;
Insert picture description here

3.7 Sliding window

Why use a sliding window?
When sending data one by one like the following figure, there will be a period of time when the host is idle, which is a waste of resources. Therefore, in order to solve the efficiency problem, a sliding window is used to solve this problem.

This transmission method is serial transmission, which is inefficient; the
sliding window uses a parallel method to transmit data, similar to a multi-threaded method, and multiple pieces of data can be sent at the same time.
Insert picture description here
Parallel data transmission mode:
window size: the maximum value of data that can be sent without waiting for a confirmation response.

There is a sending buffer here to record data information. Only the data that has been acknowledged can be deleted from the sending buffer.
The larger the window, the higher the network throughput.
Insert picture description here
Packet loss occurs in the process of using the sliding window to send data, which can be discussed in two situations:
(1) The data packet has arrived and the ACK is lost;
it does not matter if the confirmation response is lost. It can be based on the sequence number of the last confirmation response. Like the confirmation number in the figure, the next one is 601, which means that all the current packages have been accepted.
Insert picture description here

(2) The data packet is directly lost;
Insert picture description here

3.8 Flow control

The receiving end has limited receiving capacity, so we need to tell the sending end our receiving capacity, which is similar to selling tickets in scenic spots and booking rooms in hotels. If it is overcrowded, stop selling tickets. If there are too many people, limit the number of tickets sold.
Flow control will adopt the operation of letting the receiving end actively lose packets.
Flow control window: It is set based on the window size field in the TCP message, which affects the window size of the sender.

3.9 Congestion control

If the sending end's network status is unknown, sending a large number of data packets rashly will cause network congestion, so a small amount of data must be sent first to find the way.
Insert picture description here

3.10 Delayed response

It is also a mechanism for solving efficiency.

When the receiving end receives the data, it immediately responds to the ACK, and the flow control window (the buffer space of the receiving end is limited) will be smaller.

Guess you like

Origin blog.csdn.net/m0_46551861/article/details/114755917