Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the comparative analysis of the differences between TCP and UDP

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

Today, I will talk to you about the comparative analysis of the differences between TCP and UDP, which may not be well understood by many people. in order to make you understand better, the editor has summarized the following contents for you. I hope you can get something according to this article.

Write at the front:

The company is working in the direction of intelligent hardware, so it uses TCP and UDP protocols to communicate. I'll sort out the use of the two protocols on Android in a few days, but before I do that, I'd like to know the similarities and differences between the two protocols and what's worth noting. How to compare and analyze the differences between TCP and UDP, to help these more basic knowledge points, encounter the corresponding problems, can be quickly solved.

Establish connection mode

TCP: when it comes to establishing a connection with TCP, I'm sure most people can think of a word that's right-- "three-way handshake." TCP uses a "three-way handshake" to establish a connection and a "four-wave" to disconnect a connection. What does TCP do with each wave? Continue to look down:

TCP's three-way handshake and four waves

The image above clearly shows TCP's three-way handshake and four waves from the client and server point of view.

As you can see, when TCP tries to establish a connection, the three-way handshake is triggered twice by the client and once by the server.

We can first clarify what is the goal for TCP to establish a connection and initialize it. 1. Initialize resource 2. Tell each other my serial number. So the order of the three handshakes is as follows:

1) the client side first sends a SYN packet telling the Server side that my initial sequence number is X.

2) after receiving the SYN packet, the Server replied to client with an ACK confirmation packet and told client that I had received it.

3) then the server side also needs to tell the client side its initial serial number, so Server also sends a SYN packet to tell client that my initial serial number is Y.

4) after Client received it, replied to Server with an ACK confirmation packet saying that I knew it.

The 2 and 3 steps can be simplified to one step, that is, the ACK acknowledgement packet and the SYN serialization packet are sent to the Client side together. At this point, we simply explain the "three-way handshake" used by TCP to establish a connection.

UDP: we all know that TCP is a connection-oriented, reliable, orderly transport layer protocol, while UDP is a Datagram-oriented, unreliable, unordered transport protocol, so UDP doesn't make any connections at all.

Just like sending a text message, UDP only needs to know the other party's ip address and send a copy of the Datagram. As the sender, you don't need to care.

Data transmission

The difference in data transmission between TCP and UDP can reflect the biggest difference between the two:

TCP: because TCP is a protocol based on a connection between two ends, there is theoretically no limit on the size of the data stream sent. However, due to the size limit of the buffer, if you use TCP to send a large piece of data, it may be truncated into several segments, and the receiver will receive it in turn.

UDP: since UDP itself sends a Datagram, it naturally has an upper limit. What are the factors that determine the size of each Datagram sent by UDP?

The UDP protocol itself, the UDP protocol has a 16-bit UDP message length, so the UDP message length cannot exceed 2 ^ 16 = 65536.

The length of the Ethernet (Ethernet) data frame, the MTU (maximum transmission unit) of the data link layer.

UDP send cache size of socket

First, let's take a look at the first factor. The packet length of the UDP protocol is 2 ^ 16-1. The packet header of the IP protocol occupies 8 bytes, and the packet header of the UDP protocol itself takes 20 bytes, so the final length is 2 ^ 16-1-20-8 = 65507 bytes.

Just looking at the first factor is a bit idealistic, because UDP is an unreliable protocol, so we should try our best to avoid packet fragmentation during transmission. So here's a very important concept, MTU-- that is, the maximum transmission unit.

The value of MTU under Internet is 576 bytes, so the UDP protocol is used under internet. The maximum number of bytes per Datagram is 576-20-8 = 548.

TCP: let's talk about the ordering of data again.

For TCP, TCP itself has a series of complex algorithms such as timeout retransmission, error retransmission, and so on, which ensure that the data of TCP is orderly. Suppose you send data 1, 2, 3, as long as the sender and the receiver remain connected, the data received by the receiver is always 1, 2, 3.

UDP:

On the other hand, the UDP protocol is much more unrestrained, no matter how big the buffer pool is on the server side, the messages sent by the client side are always received one by one. And because of the unreliability and disorder of UDP itself, if client sends these three datagrams 1, 2, 3, the server side may receive a permutation and combination of three datagrams in any order and any number.

Reliability.

In fact, we all know that TCP itself is a reliable protocol, while UDP is an unreliable protocol.

TCP:

Many of the algorithmic mechanisms within TCP make it reliable in the process of keeping him connected. For example: TCP timeout retransmission, error retransmission, TCP flow control, congestion control, slow hot start algorithm, congestion avoidance algorithm, fast recovery algorithm and so on.

So TCP is such a protocol with complex internal principle but easy to use.

UDP:UDP is a connectionless protocol. Each Datagram sent by UDP has its own IP address and receiver's IP address. It doesn't care whether the Datagram is wrong or not, as long as it is sent out. So let's take a look at what causes UDP packet loss:

Datagram fragmentation loss: as we said earlier in the article, the size of each Datagram in UDP is the most appropriate. In fact, the size specified by the UDP protocol itself is 64kb, but there is a MTU limit at the data link layer, about the size of 5kb, so when you send a large UDP packet, the packet is shredded at the IP layer and then reassembled. This process may lead to the loss of fragmented packets. UDP has its own CRC detection mechanism, which will discard lost UDP packets.

The UDP buffer is full when the UDP buffer has been filled and the receiver has not processed this part of the UDP Datagram, then the Datagram has no place to store and is naturally discarded.

Working with scen

In the last part of the article, talk about the usage scenarios of TCP and UDP. Let's start with UDP. Many people will think that UDP is superior to TCP in terms of performance and speed. Because UDP does not need to maintain a continuous connection, nor does it need to confirm the sending and receiving packets. But in fact, after so many years of development, TCP has enough algorithms and optimizations. When the network is in a good state, the overall performance of TCP is better than that of UDP.

So when do we have to use UDP?

In the case of high real-time requirements, such as real-time meetings and real-time video, if you use TCP, when the network is not good for retransmission, the picture will definitely be delayed, or even more and more. If you use UDP, it doesn't matter if you lose a few packets occasionally, so it's better to use UDP in this case.

Multipoint communication TCP needs to maintain a long connection, so when multipoint communication is involved, it must be necessary to establish its two-way connection with multiple communication nodes, and then sometimes in the NAT environment, it is not easy for two communication nodes to establish their direct TCP connection, while UDP can send directly without maintaining the connection, so the cost will be very low and the penetration will be good. It is also right to use UDP in this case.

We have mentioned the usage scenario of UDP above, but in other cases, it is absolutely right to use TCP.

After reading the above, do you have any further understanding of the comparative analysis of the differences between TCP and UDP? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report