Joke Collection Website - Public benefit messages - Comparative analysis of differences between TCP and UDP
Comparative analysis of differences between TCP and UDP
The company is in the direction of intelligent hardware, so it uses TCP and UDP protocols for communication. I will sort out the use of these two protocols on Android in a few days, but before that, I still want to know what are the similarities and differences between the two protocols and what are worthy of attention. This paper helps these basic knowledge points by comparing and analyzing the differences between TCP and UDP, and can solve the corresponding problems quickly.
TCP:
When it comes to establishing TCP connection, I believe most people can definitely think of one word, and that is "three-way handshake". TCP establishes a connection through "three-way handshake" and then disconnects through "four-way handshake". What does TCP do in each wave? Keep looking down:
The above figure clearly shows the three-way handshake and Four Waves of TCP from the perspective of client and server.
As you can see, when TCP tries to establish a connection, three-way handshake means that the client triggered twice and the server triggered once.
We can first make clear what is the goal of TCP connection establishment and initialization. 1. Initialize resource 2. Tell each other my serial number.
Three-way handshake's order is this:
Steps 2 and 3 can be simplified into one step, that is, the ACK acknowledgement packet and SYN serialization packet are sent to the client. So far, we have simply explained the "three-way handshake" of TCP connection.
UDP:
We all know that TCP is a reliable and orderly transport layer protocol for connection, while UDP is an unreliable and disorderly transport protocol for datagram, so UDP will not establish any connection at all.
Just like texting, UDP only needs to know the ip address of the other party and then send datagrams one by one. As the sender, others don't need to care.
Regarding the difference of data transmission between TCP and UDP, the biggest difference between them can be reflected as follows:
TCP:
Because TCP is a protocol based on two-terminal connection, there is no limit to the size of the data stream sent in theory. However, due to the size limit of the buffer, if a large block of data is sent through TCP, it may be truncated into several blocks, and the receiver will receive it in turn.
UDP:
Because UDP itself sends datagrams, it naturally has an upper limit. What factors determine the datagram size that UDP sends each time?
First, let's look at the first factor. The message length of UDP itself protocol is 2 16- 1, with UDP header accounting for 8 bytes and IP header accounting for 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, and we should try to avoid packet segmentation during transmission. So there is a very important concept MTU-that is, the maximum transmission unit.
The value of MTU on the Internet is 576 bytes, so UDP protocol is used on the Internet, and the maximum number of bytes per datagram is: 576-20-8 = 548.
TCP:
Let's talk about the order of the data.
For TCP, TCP itself has a series of complex algorithms such as timeout retransmission and error retransmission to ensure that TCP data is orderly. Suppose you send data 1, 2, 3. As long as the sender and the receiver keep connected, the data received by the receiver is always 1, 2, 3.
UDP:
UDP protocol is more unrestricted. No matter how big the buffer pool on the server side is, the messages sent by the receiving client are always received one after another. In addition, due to the unreliability and confusion of UDP itself, if the client sends 1, 2 and 3, the server may receive any number of three datagrams in any order.
In fact, everyone knows that TCP itself is a reliable protocol, while UDP is an unreliable protocol.
TCP:
Many algorithms and mechanisms inside TCP make it reliable in the process of maintaining connection. For example: TCP overtime retransmission, error retransmission, TCP flow control, congestion control, slow preheating algorithm, congestion avoidance algorithm, fast recovery algorithm and so on.
Therefore, TCP is a protocol with complex internal principles but relatively simple use.
UDP:
UDP is a connectionless protocol. Each datagram sent by UDP has its own IP address and the IP address of the receiver. It doesn't care whether the datagram is wrong, as long as it is sent. So, let's look at what causes UDP packets to be lost:
In the last part of the article, the usage scenarios of TCP and UDP are discussed.
Let's talk about UDP first Many people will think that UDP is superior to TCP in performance and speed. Because UDP does not need to maintain a continuous connection and does not need to confirm sending and receiving packets. But in fact, after so many years of development, TCP has enough algorithms and optimizations, and the overall performance of TCP is better than that of UDP when the network is in good condition.
So when do we have to use UDP?
We have talked about the use of UDP above. In other cases, it is correct to use TCP. After all, there is a saying.
Write it at the back:
This paper mainly introduces the concepts of TCP and UDP, and summarizes the use of TCP and UDP from the perspective of Android.
- Previous article:OPPO third-party application file storage path
- Next article:Daqing * * * identified 13 close contacts. Close contacts in Daqing city.
- Related articles
- Iphone4 has a new mobile card, and then it can't send text messages. Ask to set the number of SMS center online. I also set the address not to be displayed. How did this come back?
- Asking for help, I have been receiving a lot of harassing calls these days, including landline phones, mobile phones and unlisted numbers.
- What is Internet Information Service (ICP)? What is short message service (SP)?
- Is the direct recruitment of Luckin Coffee¡¯s boss reliable and safe?
- What should I pay attention to when doing a part-time job?
- Why can't the mobile phone receive text messages? I can't change my mobile phone.
- Forty romantic essays for girlfriends.
- What about Wuhan Boyahong Technology Co., Ltd.?
- Talk about leaving the company
- When will 2022 Hebei Express resume the latest news?