review
The IP protocol at the network layer provides logical communication between hosts, and the service model of IP is to provide services as best as possible. This means that IP tries its best to deliver packets between hosts, but it makes no guarantees. Delivery of a segment is not guaranteed, the sequential delivery of a segment is not guaranteed, and the integrity of the data in a segment is not guaranteed. So IP is not a reliable service
The transport layer (TCP and UDP) extends the IP delivery service between two end systems to the delivery service between two processes running on the end systems. UDP, on the other hand, only provides process-to-process data delivery and error checking, so UDP is also an unreliable service
TCP vs. UDP
“TCP is connection-oriented, while UDP is connectionless”
Connection establishment refers to the establishment of a data structure to maintain the state of the interaction between the client and the server in order to maintain the connection, and the use of such a data structure to ensure the so-called connection-oriented nature.
Connection-oriented means that a connection-oriented protocol establishes a connection before an exchange occurs, such as TCP does three handshakes, while UDP does not. With UDP, there is no handshake between the sender and the receiver’s transport entity before the message is sent, so UDP is said to be connectionless. So the differences between TCP and UDP are mainly reflected in the following aspects:
- Reliable delivery. Data transmitted through TCP is error-free, not lost, not repeated, and arrives in sequence. While UDP inherits the characteristics of IP packet, it does not guarantee not to be lost, and does not guarantee to arrive in order
- Object orientation is different. TCP is oriented to byte streams. It is sent as a stream with no beginning and no end. The reason it is sending a stream is what TCP’s own state maintenance does. But UDP inherits the characteristics of IP packets and is based on packets, sent one by one and received one by one
- Congestion control. TCP has its own congestion control mechanism, and when it realizes that a packet has been dropped or that the network is down, it adjusts its behavior, such as slowing down the transmission speed. While UDP has no congestion control, it will only be issued according to the instructions of the application layer. Even if the network environment is poor, as long as the instruction is issued from the upper layer, it will be sent all the time
- Stateless. By numbering data, TCP records exactly whether or not a particular piece of data was sent, received, sent to, and received. UDP, on the other hand, is stateless and does not record information about the packets being sent
Structure of the UDP package
UDP packets are connectionless, so how do you know the destination port number when the UDP packet is first sent? For the service on the server, the port number of Zhou Zhi within 1024 is used, such as DNS server, then the source host can fill in 53 at the destination port number when sending UDP. (But it always feels that this reason is not very logical)
Actions before UDP packet parsing: After receiving a packet, the receiver first checks to see if the destination MAC address matches its own MAC address. If so, remove the MAC header and hand over the rest to the IP layer. After removing the IP header, the IP layer finds that the destination IP address is the same as its own IP address, and then checks the protocol type in the IP header, and finds that part of the IP packet is UDP protocol. The UDP package is then parsed. After parsing the UDP package, look at the destination port number in the header, and pass the data inside to an application listening on that port.
The characteristics of UDP
- Simple structure. The header of UDP consists of only the source port and destination port, as well as the message length and checksum. The rest of the header is the data part. This allows the application layer to exercise precise control
- It’s not going to make a connection, it’s going to have a port number, but when you’re listening, anyone can pass data to him, anyone can pass data to him, or even multiple people at the same time
- Connectionless state. TCP needs to maintain connection state, including receive and send caches, congestion control parameters, and sequence and acknowledgement number parameters. UDP does not maintain connection state and therefore does not have these parameters. So servers running on top of UDP can generally support more active users
- The head cost is low. UDP starts with only 8 bytes, while TCP has 20 bytes
UDP application scenarios
- Intranet with few resources and good network condition; Or applications that are not sensitive to packet loss. For example, with DHCP, IP acquisition is generally carried out on the Intranet, even if the packet does not arrive, because it is an Intranet, so it does not matter. In addition, when installing the operating system of PXE, the use of TFTP to download the operating system is also the use of UDP, because at this time the client has no operating system, has few resources, it is difficult to maintain more complex TCP
- Apps that can be broadcast without the need for one-to-one communication and connection. UDP is connectionless, so it can be used in broadcast or multicast protocols. The DHCP mentioned above is broadcast
- Need to process fast, low latency, can tolerate a small number of packet loss, but require that even if the network is congested, the packet must be delivered. UDP is simple in structure and fast in processing, unlike TCP, which guarantees order and requires retransmission, which adds latency. And UDP has no congestion control and will issue packets even if the network is in bad condition. At this point, TCP will slow down the transmission speed, making the application become more sluggish.
Because of the simple structure of UDP, the application can do some of the work itself, and take advantage of the fast, low delay, simple structure of UDP to develop a custom application:
-
Access to web pages or apps. The original web pages and apps were based on the HTTP protocol, which is based on TCP. Multiple interactions are required to establish the connection, resulting in high latency. In addition, when the phone is in the process of moving, TCP may be disconnected, and reconnection will increase the delay. And the current HTTP protocol, often take multiple data channels to share a connection, which was originally to speed up the transmission speed, but TCP’s strict sequence policy makes even if the shared channel, the former one does not come, the latter one and the former one even if it does not matter, also have to wait, the delay will increase
The QUIC protocol used in Chrome uses UDP as the supporting transport protocol and achieves reliability in the application layer protocol on top of UDP
-
Streaming media protocol. The RTMP protocol, which is also based on TCP, is used for live broadcasting. TCP’s strict guarantee of sequential data is unsuitable for live streaming, because when old frames are lost, they are lost, and users don’t care if they send them back. They want to see the new ones. If you don’t come and wait, you’ll be stuck, and you won’t be able to see anything new. So live broadcast, real-time comparison is more important, would rather lose the bag, also don’t get stuck. When the network is not good, the TCP protocol will voluntarily slow down the transmission speed, which makes the already slow video even more slow. In fact, the application layer should immediately retransmit, rather than give in voluntarily
The following is a description of video frames. Videos are formed by one picture after another. A group of pictures are quickly played to form a video. A picture is called a frame in a video. When you save a video, you don’t save every frame in its entirety, but you save the differences from the previous frame to reduce the storage footprint. In theory, you could just keep the first frame of the video, and keep all the changes from the first frame. In practice, however, the entire frame is saved every few frames in order to prevent errors in transmission.
This principle is equivalent to saving the array of [1000, 1001, 1002, 1003, 1004]. If the array is saved in the form of [1000, 1, 1, 1, 1], all the items after 1000 are saved in the size gap between the previous item, then a lot of storage space will be saved
- Real-time games. Games have a high requirement for timeliness. In real-time games, the client and the server need to establish a long connection to ensure real-time transmission. Maintaining a TCP connection requires maintaining some data structures in the kernel, so there is a limit to how many TCP connections a single machine can support, and as the number of players increases, one machine is not enough. UDP, on the other hand, is connectionless and, before the introduction of asynchronous IO, was often a strategy for dealing with massive client connections. In addition, TCP is a strong order problem. In the battle game, the client sends the location of the mouse and keyboard to the server, and the server will process all the scenes sent by each user, and then return them to the client. The client analyzes the response and renders the latest scenes to show to the player. If a packet is lost, everything has to stop and wait for the packet to be resold. The client will be waiting to receive data, but the player does not care about out-of-date data. When the real-time requirements of the game are relatively strict, the use of custom reliable UDP protocol, custom retransmission strategy, can reduce the delay caused by packet loss to a minimum, as far as possible to reduce the impact of network problems on the game.
- IoT Internet of Things. On the one hand, there are few terminal resources in the field of the Internet of Things, which is probably just an embedded system with a very small memory, and the cost of maintaining the TCP protocol is too high. On the other hand, the Internet of Things also requires high real-time performance, while TCP still causes large delay due to the above reasons
- The field of mobile communication. In 4G networks, GTP-U, the protocol for accessing mobile traffic, is based on UDP. Because the mobile network protocol is more complex, and the GTP protocol itself contains complex mobile phone online and offline communication protocol. If based on TCP, the TCP mechanism is quite redundant