User Datagram Protocol
lastmod 2025-02-27created 2017-01-1311 edits
The User Datagram Protocol (UDP) is one of the core protocols of the Internet protocol suite. It was designed by David Reed in 1980, is currently defined as a standard in the IETF's RFC 768, and, together with TCP, is used to exchange short messages known as datagrams. UDP is also sometimes called the Universal Datagram Protocol.
UDP's transmission method is so simple that the reliability of the service is low: datagrams may arrive out of order, be duplicated, or even be dropped without notice. UDP is generally assumed to run in applications that do not need error checking and correction.
Network applications that use UDP include the Domain Name Service (DNS), IPTV, Voice over Internet Protocol (VoIP), TFTP, IP tunnels, and many online games.
Comparison of UDP and TCP
TCP first establishes a connection between the two ends that will exchange data and transmits data in both directions over that connection, whereas UDP transmits information in one direction without establishing a connection and without a step to check that the receiver is ready to receive data.
- Reliability - TCP acknowledges message receipt, but UDP cannot confirm whether the receiver received a message.
- Ordering - TCP reassembles messages to guarantee the order in which they were sent, but with UDP the order in which messages arrive cannot be predicted.
- Load - Generally faster than TCP with less overhead.
Packet structure
| Offsets | Octet | 0 | 1 | 2 | 3 | ||||||||||||||||||||||||||||
| Octet | Bit | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 |
| 0 | 0 | Source port | Destination port | ||||||||||||||||||||||||||||||
| 4 | 32 | Length | Checksum | ||||||||||||||||||||||||||||||
Brief
UDP adds two features to IP.
- Port numbers
- Checksum
Like IP, UDP is connectionless and does not pursue reliability. TCP avoids IP fragmentation with the PMTUD algorithm, but UDP has no such mechanism, so IP fragmentation often occurs. UDP-based applications usually avoid IP fragmentation by using a datagram size smaller than 576 bytes, the minimum IPv4 reassembly buffer size. Of the 576 bytes, 8 bytes are used by the UDP header and 20 bytes by the IP header, so the usable size for UDP is 548.
UDP vs TCP
- [Recommended] UDP Socket-1. TCP and UDP
TCP first establishes a connection between the two ends that will exchange data and transmits data in both directions over that connection, whereas UDP transmits information in one direction without establishing a connection and without a step to check that the receiver is ready to receive data.
- Reliability - TCP acknowledges message receipt, but UDP cannot confirm whether the receiver received a message.
- Ordering - TCP reassembles messages to guarantee the order in which they were sent, but with UDP the order in which messages arrive cannot be predicted.
- Load - Generally faster than TCP with less overhead.
UDP packet fragmentation
Q:
I've read a number of articles about UDP packet sizes but have been unable to come to a conclusion on whats correct.
A number of services restrict the largest UDP packet to 512 bytes (like dns)
Given the minimum MTU on the internet is 576 , and the size of the IPv4 header is 20 bytes, and the UDP header 8 bytes. This leaves 548 bytes available for user data
Would I be able to use packets up to the size of 548 without packet fragmentation? Or is there something the creators of DNS knew about, and that why they restricted it to 512 bytes.
Could I even go higher than 548 bytes safely?
A:
It is true that a typical IPv4 header is 20 bytes, and the UDP header is 8 bytes.
However it is possible to include IP options which can increase the size of the IP header to as much as 60 bytes.
In addition, sometimes it is necessary for intermediate nodes to encapsulate datagrams inside of another protocol such as IPsec (used for VPNs and the like) in order to route the packet to its destination.
So if you do not know the MTU on your particular network path, it is best to leave a reasonable margin for other header information that you may not have anticipated.
A 512-byte UDP payload is generally considered to do that, although even that does not leave quite enough space for a maximum size IP header.
UDP Broadcast
UDP Multicast
See also
Favorite site
Guide
- What would happen if we didn't use TCP or UDP? | GeekNews
- [Original] Hawzen/hdp: What would happen if we didn't use TCP or UDP?
- Conclusion: using TCP and UDP is best
References
-
Things_to_consider_when_using_UDP_-Meetup-_TOAST_Cloud.pdf ↩
- ComputerNetwork:Basic
- Data Distribution Service
- Hole punching
- Hypertext Transfer Protocol
- Interactive Connectivity Establishment
- Network monitoring
- Network Programming
- Network socket
- OSI Model
- Port
- Programming
- QUIC
- TCP hole punching
- Technical Terms
- TOAST Haste
- Transmission Control Protocol
- UDP hole punching
- WebRTC