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 difference and principle between TCP and UDP in computer network

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article will explain in detail the differences and principles of computer network TCP and UDP. The content of the article is of high quality, so the editor will share it with you for reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.

1. Concept

UDP and TCP in the network communication using TCP/IP architecture, the frequency of these two protocols is second only to the IP protocol of the Internet layer. Some protocols in the application layer of TCP/IP architecture need to use the services provided by TCP in the transport layer, while others need to use the services provided by UDP in the transport layer. UDP is the abbreviation of user Datagram protocol and TCP is the abbreviation of transmission control protocol.

2. From whether it is connection-oriented or not

As shown in the following figure, two minicomputer icons represent two hosts on the Internet, and the ordinate is time.

2.1 、 UDP

If two hosts use UDP protocol to communicate at the transport layer, both sides can send data at any time, so UDP is connectionless.

2.2 、 TCP

In the case of using TCP protocol, in the communication using TCP protocol, both parties must use three-message handshake to establish TCP connection before data transmission. After the TCP connection is established successfully, the data transmission can be carried out. After the data transmission is completed, you must use four messages to wave to release the TCP connection. It should be noted that the so-called connections here refer to logical connections, not physical connections. To sum up, UDP is connectionless, while TCP is connection-oriented.

3. From the number of connected objects, 3.1and UDP

The following figure shows four hosts on a local area network that communicate using the UDP protocol. Any one of these hosts can send broadcasts to the other three hosts, multicast to a multicast group, or unicast to a host. In other words, UDP supports unicast, multicast and broadcasting, in other words, UDP supports one-to-one, one-to-many and one-to-all communications.

One host sends a broadcast to the other three hosts:

Send multicast to a multicast group:

Send unicast to a host:

3.2 、 TCP

Let's take a look at the use of TCP protocol. In the communication using TCP protocol, both parties must use three-message handshake to establish TCP connection before data transmission. After the TCP connection is established successfully, it seems that there is a reliable communication channel between the two parties, and the two sides use this reliable channel based on the TCP connection to communicate. Obviously, TCP only supports unicast, that is, one-to-one communication.

Three-message handshake to establish a TCP connection:

Reliable channels based on TCP connections:

4. From the point of view of the processing of application messages, 4.1and UDP

Let's take a look at the use of the UDP protocol. The sender's application process delivers the application layer message to the UDP,UDP of the transport layer and directly adds a UDP header to the application layer message, making it a UDP user Datagram, and then sending it. It is important to note that for the sake of simplicity, we ignore the processing of the layers below the transport layer.

After receiving the UDP user Datagram, the UDP of the receiver removes the UDP header and delivers the application layer message to the application process. In other words, UDP neither merges nor splits the messages handed over by the application process, but retains the boundaries of these messages. In other words, UDP is application message oriented.

4.2 、 TCP

Let's take a look at the use of TCP protocol.

Sender:

1. The sender's TCP regards the data blocks delivered by the application process as a series of unstructured byte streams, and TCP does not know the meaning of these byte streams with transmission.

2. The sender numbers them and stores them in their own sending cache.

3. According to the sending policy, TCP extracts a certain number of bytes from the sending cache to construct the TCP message segment and send it.

Recipient:

1. On the one hand, the TCP of the receiver takes the data payload part from the received TCP message segment and stores it in the receiving cache, on the other hand, it delivers some bytes in the receiving cache to the application process.

2. TCP does not guarantee that there is a corresponding size relationship between the data blocks received by the receiving application process and the data blocks sent out by the sender application process (for example, the sender application process gives a total of 10 data blocks to the sender's TCP, but the receiver's TCP may use only four data blocks and deliver the received byte stream to the upper application process. However, the byte stream received by the receiver must be exactly the same as the byte stream sent by the sender application process. Of course, the receiver's application process must be able to identify the received byte stream and restore it to meaningful application layer data.

Therefore, TCP is byte flow oriented, which is the basis for TCP to achieve reliable transmission, flow control and control.

It is important to note that in order to highlight the main points of the diagram, we have drawn the data flow in only one direction. In the actual network, both ends of the TCP connection can send and receive TCP message segments at the same time, that is, full-duplex communication. In addition, the data portion of the figure contains only a few bytes, and it is common for an TCP message segment to contain thousands of bytes.

5. From the point of view of providing transmission services to the upper layer, 5.1, UDP

The Internet layer of TCP/IP architecture provides connectionless and unreliable transport services to its upper layer. When the transport layer uses UDP protocol, it also provides connectionless and unreliable transport services to its upper layer.

The sender sends the UDP user Datagram to the receiver. If the user Datagram is disturbed during transmission, the receiver UDP can check the occurrence of the object code through the value of the checksum field in the header of the Datagram, but just discard the Datagram and do nothing else.

The sender sends the UDP user Datagram to the receiver. If the Datagram is discarded by a router in the Internet, the sender UDP does not do any processing, because UDP provides connectionless and unreliable transmission services to the upper layer.

Therefore, UDP does not care about the object code and loss of UDP user Datagram. Based on this feature of UDP, UDP is suitable for real-time applications, such as IP telephone, video conference and so on.

5.2 、 TCP

Looking at the use of TCP protocol, although the IP protocol in the Internet layer provides connectionless and unreliable transmission services to the upper layer, that is to say, IP Datagram may be lost and error in the process of transmission, as long as the transport layer uses TCP protocol, it can provide connection-oriented reliable transmission services to the upper layer.

We can think of it as the transceiver using the TCP protocol. Based on the reliability of the TCP connection, both sides carry out data transmission without error loss, disorder, repetition and other transmission errors. Therefore, TCP is suitable for applications that require reliable transmission, such as file transfer.

6. From the first format, 6. 1, UDP

An UDP user Datagram consists of a header and a data payload. The format of the header is as follows:

There are only four fields, each with a length of 2 bytes. Because UDP does not provide reliable transmission services, it only adds ports to distinguish application processes on the basis of the Internet layer, so its header is very simple, only 8 bytes.

6.2 、 TCP

A TCP message segment consists of a hand and a data payload, and its header format is shown in the following figure:

This is much more complex than the header of UDP user Datagram, with a minimum length of 20 bytes and a maximum length of 60 bytes. This is because the header of TCP will naturally be more complex if it wants to achieve reliable transmission, flow control and other services, with more fields and longer length.

7. Summary

UDP and TCP comparison table:

Whether the type is connection-oriented transmission reliability transfer form of transfer efficiency required resource application scenario the first byte TCP is reliable byte flow slow multi-file transfer, mail transfer 20,60 bytes UDP No unreliable data message segments fast less instant messaging, domain name conversion 8 bytes

Comparison of UDP and TCP:

On the computer network TCP and UDP differences and principles of what is shared here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

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

Development

Wechat

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

12
Report