- Published on
UDP
- Authors
- Name
- Bowen Y
Basic Characteristics of UDP
- Connectionless Protocol: UDP does not establish a connection before sending data. It sends data without ensuring that the receiver is ready or available to receive it, which contrasts sharply with TCP (Transmission Control Protocol), a connection-oriented protocol.
- No Guarantee of Delivery: UDP does not guarantee that data sent will reach its destination, as it does not track delivery. There is no acknowledgment mechanism that the data has been received.
- No Error Correction: UDP does not offer any error correction. If a packet is lost or arrives with errors, UDP will not attempt to resend it. Error handling (if any) must be implemented at the application level.
- No Order Guarantee: Data packets (datagrams) may arrive in a different order than they were sent. It's up to the application to reorder them if necessary.
Structure of a UDP Datagram
A UDP datagram consists of a header and a data section. The UDP header is very simple compared to the TCP header:
- Source Port (16 bits): The port number of the sending process.
- Destination Port (16 bits): The port number of the receiving process.
- Length (16 bits): The length of the UDP header and data. The minimum value is 8 bytes (the size of the header).
- Checksum (16 bits): Used for error-checking of the header and data. This field is optional in IPv4 (may be zero) but mandatory in IPv6.
Advantages of UDP
- Speed: Because it lacks the overhead of connection setup, acknowledgments, and other features of TCP, UDP is typically faster and more suitable for applications that need rapid transmission of data, such as video streaming or online gaming.
- Simple: The simplicity of UDP reduces the resource requirements of the application, both in terms of code complexity and system overhead.
- Real-time Applications: UDP is often used in real-time applications where timely delivery of data is more critical than perfect delivery.
Drawbacks
- Unreliable: The lack of delivery and order guarantees, and error correction can be problematic for applications requiring reliable data transmission.
- Security: UDP is susceptible to spoofing and DoS attacks because it does not verify the source of the datagrams.
Conclusion
UDP is a fundamental protocol that offers simplicity and efficiency where the integrity and sequence of the data packets can be managed or tolerated at the application level. It is particularly useful for services where the speed of communication is more crucial than the precision of the data received.
Real-world Scenarios for Using UDP
Video Streaming Services: For live video streaming, such as sports events or concerts, UDP is preferred because it allows for continuous data flow without the interruption that can occur with TCP’s requirement for acknowledgment and retransmission. This keeps the video playing smoothly even if some packets are lost.
Online Multiplayer Games: In fast-paced online games, the speed of updates is crucial. UDP allows game state updates (like player positions and actions) to be sent quickly and frequently. While some information might be lost, the next update can make the corrections, which is acceptable as slight inaccuracies are less critical than real-time interaction.
Voice over IP (VoIP): Applications like Skype or Zoom may use UDP because it reduces latency in voice transmission. If packets are lost, they are usually not retransmitted, as doing so would cause delays that might make conversation difficult or unnatural.
DNS Queries: UDP is used for DNS queries because they typically involve single request-response actions, and the data size is small. If a response is lost, the query can simply be resent, and using UDP avoids the overhead of setting up and tearing down a TCP connection.
IoT Devices: Many IoT applications use UDP because these devices often operate on limited power and need to transmit small amounts of data without the overhead of establishing a connection.
Unlike TCP, UDP is a message-oriented protocol
UDP operates with datagrams, which are discrete packets of data. Each datagram is a self-contained message with a defined beginning and end.
Unlike TCP, which treats data as a continuous stream of bytes, UDP preserves message boundaries. Each datagram sent by the sender is received as a distinct, complete message by the receiver.
UDP is connectionless, meaning that it does not establish or maintain a connection between the sender and receiver. Each datagram is sent independently, and there is no guarantee of delivery, order, or error correction.
UDP does not segment data into smaller units or reassemble them. Each datagram must fit within the size limits imposed by the network (usually up to 65,535 bytes, including the UDP header).
Handling Large Message
UDP (User Datagram Protocol) is designed to send discrete packets of data called datagrams, each of which must fit within the size limits imposed by the protocol and the underlying network infrastructure. The maximum size of a UDP datagram, including the UDP header, is 65,535 bytes. This includes:
UDP Header: 8 bytes Payload: Up to 65,527 bytes
Therefore, a single UDP datagram cannot exceed 65,535 bytes. If an application needs to send a message larger than this, it must split the message into smaller chunks, each fitting within the size limit, and send each chunk as a separate UDP datagram.
When an application needs to send data larger than 65,535 bytes using UDP, it must implement its own mechanism to fragment and reassemble the data.
Simple Video Streaming Example
- Video Encoding(Sender)
- Fragmentation into Chunks
- Adding Sequence Numbers
- Creating UDP Datagrams
- Receiving UDP Datagrams(Receiver)
- Buffering
- Handling Loss and Out-of-Order Packets
- Reconstructing Frames
- Decoding and Playback