UDP Protocol Guide

User Datagram Protocol - Fast, Connectionless Communication

What is UDP?

UDP (User Datagram Protocol) is a connectionless, lightweight transport protocol that provides fast data transmission with minimal overhead. It's ideal for real-time applications where speed is more important than reliability.

Fast

Minimal overhead and no connection establishment delays.

Connectionless

No handshake required - data is sent immediately.

Best Effort

No guarantee of delivery, ordering, or error checking.

UDP Communication Flow

UDP Header Structure

UDP Characteristics

Advantages

  • Low latency - no connection setup
  • Minimal overhead - only 8-byte header
  • No flow control - constant data rate
  • Multicast and broadcast support
  • Simple implementation

Disadvantages

  • No delivery guarantee
  • No ordering guarantee
  • No error detection
  • No congestion control
  • No flow control

UDP vs TCP Comparison

Feature UDP TCP
Connection Connectionless Connection-oriented
Reliability Best effort Guaranteed
Ordering Not guaranteed Guaranteed
Speed Very fast Slower
Header Size 8 bytes 20+ bytes
Error Checking Optional checksum Full error detection
Use Cases Gaming, streaming, DNS Web, email, file transfer

Common UDP Applications

DNS (Port 53)

Domain name resolution - fast lookups are more important than reliability.

// DNS Query
Query: example.com
Response: 192.168.1.1
If lost, client retries

Gaming (Port 27015)

Real-time multiplayer games - low latency is critical.

// Game Packet
Player position: (x, y, z)
Action: shoot, move, jump
Timestamp: 1234567890

Streaming (Port 1234)

Video/audio streaming - missing frames are acceptable.

// Video Frame
Frame data: [binary]
Sequence: 001, 002, 003...
If lost, skip to next

Common UDP Ports

Network Services

  • 53 - DNS
  • 67 - DHCP Server
  • 68 - DHCP Client
  • 123 - NTP

Gaming

  • 27015 - Steam
  • 25565 - Minecraft
  • 7777 - Terraria
  • 27005 - Source Engine

Streaming

  • 5004 - RTP
  • 5005 - RTCP
  • 1935 - RTMP
  • 8080 - HTTP Alt

System Services

  • 161 - SNMP
  • 162 - SNMP Trap
  • 514 - Syslog
  • 69 - TFTP

UDP Programming Example

Python UDP Server

import socket
# Create UDP socket
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.bind(('localhost', 8080))
while True:
data, addr = sock.recvfrom(1024)
print(f"Received: {data} from {addr}")
sock.sendto(b"Hello Client", addr)

Python UDP Client

import socket
# Create UDP socket
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
# Send message
message = b"Hello Server"
sock.sendto(message, ('localhost', 8080))
# Receive response
data, addr = sock.recvfrom(1024)
print(f"Received: {data} from {addr}")