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

Example Analysis of udp programming

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

Share

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

Editor to share with you the example analysis of udp programming, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

What is UDP?

UDP is an acronym for User Datagram Protocol (user Datagram Protocol). It is a simple protocol as simple as the UDP specification RFC0768.

UDP is a transport layer protocol that works on the IP layer. UDP has two main extensions to IP:

Extending the port number allows IP datagrams to be multiplex to user processes. The extended checksum provides the check of data errors in the process of network transmission. IP provides a best-effort, connectionless Datagram delivery service. IP routing and packet forwarding based on IP address can transmit an IP Datagram from one host to another host in the network, and the IP address determines which host the IP Datagram will be sent to. Therefore, IP provides host-to-host Datagram transmission service.

After the IP Datagram arrives at the destination host, the IP module implemented in the kernel layer will be responsible for receiving the IP Datagram on the network card, but multiple processes are usually running on the host at the same time. Which process should the IP Datagram be handed over to handle? IP can't handle it.

The port number (at the head of the UDP) determines which process on the host will process the Datagram. Therefore, UDP provides end-to-end services for applications running on the end host.

Characteristics of UDP

UDP is connectionless, and datagrams can be sent directly without establishing a connection before communication, while TCP is connection-oriented. UDP does not provide error correction, but UDP provides error detection (end-to-end checksum). UDP does not do deduplication. UDP does not do flow control. UDP does not do congestion control, there is no protocol mechanism to prevent the negative impact of high-speed UDP traffic on other network users. UDP does not guarantee the order in which datagrams are submitted for application. UDP is not reliable, UDP is only responsible for sending the data that the application sends to the IP layer, and can not guarantee that the Datagram will arrive at the destination. Reliable delivery needs to be realized by the application. UDP supports multicast delivery. UDP is a transport layer protocol that preserves message boundaries.

Message boundary

Each time the application requests UDP output, it generates a UDP Datagram, which sends an IP Datagram, while the receiver receives a complete UDP Datagram (if any) for each UDP receive requested, unlike TCP for data flow.

Suppose that host A sends data to host B twice, the first 4-byte "abcd" and the second 3-byte "xyz", and host B receives twice, returning "abcd" and "xyz" messages, or "xyz" and "abcd" messages (the order is not important), then this is the reserved message boundary.

UDP is a transport layer protocol that preserves message boundaries. Applications using UDP communication will generate an IP Datagram (regardless of fragmentation) each time, which restricts that the amount of data sent can not be greater than MTU (the maximum transmission unit). The receiver will return the full load of UDP datagrams every time, and there will be no return of half of the Datagram load.

While TCP is a streaming protocol that does not retain message boundaries, the number of calls sent by the sender and the amount of data sent each time have no corresponding relationship with the number of calls to receive by the receiver and the amount of data received each time, so applications that use TCP need to deal with message boundaries.

UDP Datagram encapsulation format

The IPv4 Protocol (Protocol) field uses a value of 17 to identify that the UDP,UDP Datagram header is usually 8 bytes, followed by the IPv4 header followed by the UDP header, followed by the UDP data Payload, if any. The UDP header corresponding to the IPv4 packet consists of a source port number, a destination port number, a length, and a checksum, and each field is 2 bytes. 1. The port number is a purely abstract identity, which is not associated with any physical entity to help the protocol distinguish between sending and receiving processes. After receiving the IP Datagram from the Nic and identifying the UDP Datagram (IP Datagram header protocol field value = 17), the kernel layer of the receiver will map the UDP Datagram to the corresponding process according to the destination port number of the UDP header, and leave the UDP Datagram to the corresponding process for processing. This mapping relationship is managed and maintained by the system kernel. The destination port number is required, but the source port number is optional, and the source port number can be set to 0 if the Datagram sender does not need a reply.

Because the IP layer distributes the incoming IP Datagram to a specific transport protocol (TCP or UDP, etc.) according to the protocol type field of the IP header, to the transport protocol layer, and then distributes the protocol data to different processes according to the port number. Therefore, the port number is protocol independent, and the same port number of different protocols will not cause distribution confusion.

For example, there is no problem that two network service processes on a machine use the same IP address and port number, but one uses the TCP protocol and the other uses the UDP protocol.

2. The length field is the total length of the UDP header and UDP data in bytes, because the length of the UDP header is 8 and the UDP Datagram of empty data is allowed, which means that the minimum value of the length field is 8. The UDP length value is redundant because it can be derived from the total length of the IP Datagram minus the length of the IP header.

3. Checksum, covering the UDP header, UDP data and a pseudo header, which is calculated by the initial sender and checked by the final destination, which is used to determine whether the Datagram is wrong during network transmission, for example, a bit has changed from 1 to 0.

How to achieve reliable transmission of applications using UDP

It is well known that UDP is unreliable and does not guarantee order.

1. What is unreliable? a sends a UDP Datagram to B. the UDP Datagram may not be delivered correctly to the receiver B, but the packet may be lost due to various reasons such as network quality. IP Datagram is delivered as best as possible.

Is there any way to guarantee that the UDP sent will reach the destination? Sorry, no guarantee, no way.

What does the reliable transmission provided by TCP mean? reliable transmission provided by TCP does not mean no packet loss, because TCP also relies on IP (IP cannot be relied on) for Datagram delivery. TCP reliability means that lost packets are retransmitted until they are delivered correctly before the next Datagram is transmitted.

So how does TCP achieve reliable transmission? Very simple, receipt confirmation (ack) + lost packet retransmission. Therefore, if UDP wants to provide reliable transmission, we can also refer to the implementation mechanism of TCP, but TCP implements the kernel layer, while applications based on UDP can achieve reliable transmission to the application layer. To confirm receipt and retransmit lost packets, you need some additional information, such as the serial number of the packet. You can put it in Payload and agree on the structure and layout of the additional information in Payload.

2. What is not guaranteed order? a sends two UDP datagrams to B, and the two UDP datagrams will be encapsulated into two IP datagrams and transmitted through the IP protocol. Because the two IP datagrams are routed independently, which comes first? Not necessarily, it depends on the mood.

Is there any way to ensure that UDP datagrams arrive at the destination in the order in which they are sent by the sender? Also Sorry, can't do it.

Therefore, the ordering provided by TCP only reorders IP datagrams according to the sending order at the receiving end. Obviously, if UDP wants to support reordering, it also needs some additional information, which can only be carried through payload, not like TCP (some fields in the header of TCP are used for reordering at the receiving end).

To sum up, UDP only provides end-to-end services for applications on the simplest end host. If you want to provide other features, please refer to TCP's ideas to implement them.

This is good: because it is simple, the cost is very small. In some application scenarios, packet loss and disorder can be tolerated, and UDP is very suitable. Porsche is good, but you should use a tractor to pull bricks.

There are not many API for UDP Socket network programming, socket () is used to create sockets, close () is used to close sockets, sendto () is used to send data, and recvfrom () is used to receive data.

Bind (), as its name implies, is binding. TCP can be bound, and so can UDP. Using bind for UDP is tantamount to telling the kernel that this socket is related to the remote end of the network

Before bind, you can only go through the sendto () interface (specify the destination by parameter). The UDP socket recv () returns the data part (Payload) of the UDP Datagram, excluding the UDP Datagram header, because the fields in the UDP header are used for distribution or verification and do not need to be passed through to the application.

For the network application Server/Client developed using UDP sockets, the operation and flow related to network IO are shown below:

The above is all the content of this article "sample Analysis of udp programming". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

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