Are you an LLM? You can read better optimized documentation at /en\wiki\UDP.md for this page in Markdown format
UDP
UDP (User Datagram Protocol) is a simple, connectionless transport protocol. Compared with TCP it is lighter and lower latency, but it offers no reliability guarantees. It suits applications that care more about speed and efficiency than about perfect delivery.
Characteristics
- Connectionless: datagrams are sent without establishing a connection, which removes setup cost and latency.
- Unreliable: no ordering, no integrity guarantee and no retransmission — packets may be lost, duplicated or reordered.
- Lightweight: the header is only 8 bytes, far smaller than TCP's, which suits latency-sensitive applications.
- Broadcast and multicast: UDP can send to many destinations at once.
Packet layout
A UDP datagram is simple:
- Source port (16 bits): the sender's port.
- Destination port (16 bits): the receiver's port.
- Length (16 bits): header plus payload.
- Checksum (16 bits): error detection.
plaintext
+-------------------+-------------------+
| Source Port | Destination Port |
+-------------------+-------------------+
| Length | Checksum |
+-------------------+-------------------+
| Data (variable) |
+---------------------------------------+Use cases
UDP is used where latency matters more than reliability:
- Real-time audio and video: VoIP, video conferencing and live streaming tolerate some loss in exchange for low latency.
- Online games: fast, responsive communication where occasional loss is acceptable.
- DNS queries: the domain name system uses UDP for fast lookups.
- IoT devices: many devices use UDP for lightweight, low-latency telemetry.
Trade-offs
Advantages
- Low latency, thanks to no connection setup and a tiny header.
- Minimal overhead.
- Broadcast and multicast support, useful for one-to-many delivery.
Drawbacks
- Unreliable: no acknowledgements or retransmission.
- No flow control, so heavy senders can congest the network.
Example: UDP in Python
A minimal client and server.
Server
python
import socket
# create a UDP socket
server_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
server_socket.bind(('localhost', 12345))
print("UDP server listening on port 12345...")
while True:
data, addr = server_socket.recvfrom(1024) # receive data
print(f"Received from {addr}: {data.decode()}")
response = f"Received: {data.decode()}"
server_socket.sendto(response.encode(), addr) # send the replyClient
python
import socket
# create a UDP socket
client_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
server_address = ('localhost', 12345)
message = "Hello, UDP Server!"
client_socket.sendto(message.encode(), server_address) # send data
data, server = client_socket.recvfrom(1024) # receive the reply
print(f"Reply from server: {data.decode()}")
client_socket.close()Summary
UDP is a simple, fast, low-latency transport protocol for real-time applications such as audio and video, gaming and DNS. It does not guarantee delivery, but its small footprint plus broadcast and multicast support make it the right tool for many workloads.
