UDP & Datagrams in C: High-Speed Connectionless Networking, Broadcasting & QUIC

UDP & Datagrams in C: High-Speed Connectionless Networking, Broadcasting & QUIC
Table of Contents
- TCP vs UDP: The Fundamental Trade-off
- UDP Socket Lifecycle: No Connection State
- Building a UDP Echo Server
- UDP Client: sendto and recvfrom
- Broadcasting: Sending to All Hosts on a Network
- Multicasting: Selective Group Communication
- Implementing Reliable UDP: Sequence Numbers and ACKs
- UDP for Real-Time Games: The State Snapshot Model
- DNS: UDP in Production (Port 53)
- QUIC: The Future of UDP (HTTP/3)
- Frequently Asked Questions
- Key Takeaway
TCP vs UDP: The Fundamental Trade-off
| Application | Protocol | Reason |
|---|---|---|
| Web (HTTP/1.1, HTTP/2) | TCP | Reliability critical |
| DNS lookups | UDP | Single request-response; fast |
| Video streaming (HLS/DASH) | TCP | Buffered; stall is OK |
| Live video (WebRTC, RTSP) | UDP | 100ms latency budget |
| Online gaming (position) | UDP | Stale packets worthless |
| VoIP (voice call) | UDP | Jitter tolerated; gaps OK |
| HTTP/3 (QUIC) | UDP | Custom reliability on UDP |
| DHCP | UDP | Broadcast; client has no IP yet |
UDP Socket Lifecycle: No Connection State
Unlike TCP, UDP has no connection establishment or teardown. The simplified lifecycle:
No listen(), no accept(), no connect() (optional for "connected" UDP). This simplicity is what makes UDP fast — there's no state machine to maintain in the kernel.
Building a UDP Echo Server
UDP Client: sendto and recvfrom
Broadcasting: Sending to All Hosts on a Network
UDP broadcasting sends a single packet to all hosts on a subnet simultaneously:
Applications: DHCP (clients broadcast to find a DHCP server), UPnP device discovery, NetBIOS name resolution, Wake-on-LAN packets.
Multicasting: Selective Group Communication
Multicast is more targeted than broadcast — only hosts that have subscribed to the multicast group receive the packets. IPv4 multicast addresses are in the 224.0.0.0/4 range:
Multicast is used by: streaming protocols (IPTV, live sports), financial market data feeds (Bloomberg, Nasdaq quote dissemination), and cluster node discovery (etcd, Consul).
Implementing Reliable UDP: Sequence Numbers and ACKs
When you need UDP's speed but TCP's reliability, implement it yourself:
This is essentially what QUIC (the protocol behind HTTP/3) does at a much more sophisticated level — connection multiplexing, congestion control, and encryption all implemented on top of UDP at the application layer.
UDP for Real-Time Games: The State Snapshot Model
Online games like first-person shooters use UDP because a lost position packet is irrelevant — the next packet (arriving 16ms later) will have newer data:
The key insight: game state packets contain the full current state, not a delta. If a packet is lost, the next packet restores full state — no need to retransmit the lost one.
DNS: UDP in Production (Port 53)
DNS uses UDP for most queries:
- Query packet: ~40-60 bytes typically.
- Response packet: ~100-500 bytes for most lookups.
- If response > 512 bytes, DNS falls back to TCP.
- The entire round trip (query + response) costs one UDP exchange — no TCP handshake.
QUIC: The Future of UDP (HTTP/3)
QUIC, standardized as RFC 9000 and implemented by Google, Cloudflare, and all major browsers, multiplexes HTTP streams over UDP:
- No head-of-line blocking: In HTTP/2 over TCP, one lost packet blocks all streams. In QUIC, each stream independently handles retransmission.
- 0-RTT connection: QUIC combines TLS handshake with connection establishment — first request can be sent in 0 round trips for known servers.
- Connection migration: QUIC connections survive IP address changes (e.g., switching from Wi-Fi to 4G) because they're identified by a connection ID, not by (source IP, source port).
QUIC's entire reliability, ordering, and multiplexing layer is implemented in C/C++ at the application layer, on top of raw UDP sockets.
Frequently Asked Questions
When should I choose UDP over TCP? Use UDP when: latency is more important than reliability (live video, gaming, trading), the application-level data naturally overwrites stale data (position updates, sensor readings), you're implementing a custom protocol that already handles reliability (QUIC, SRTP, RTP), or you need broadcast/multicast (service discovery, DHCP).
Can UDP accidentally deliver duplicate packets? Yes — UDP does not filter duplicate packets. Network equipment can duplicate packets, and routers may send a packet on multiple paths. Your application protocol must handle duplicates by checking the sequence number.
Is UDP faster than TCP for large file transfers? Usually not — TCP's congestion control and buffer management are highly optimized for throughput. Tools like iPerf3 consistently show TCP matching or exceeding naive UDP for bulk transfers. UDP only wins for latency-sensitive workloads where TCP's retransmission delays are unacceptable.
What is the maximum size of a UDP datagram? The maximum UDP payload is 65,507 bytes (16-bit length field minus IP and UDP headers). However, packets larger than the network's MTU (~1,472 bytes for typical Ethernet after headers) will be fragmented at the IP layer, reducing efficiency. Production UDP applications typically limit to 1,400 bytes per datagram.
Key Takeaway
UDP is the Express Lane of Networking. Its connectionless model, no-handshake simplicity, and broadcast/multicast capabilities make it indispensable for real-time systems where TCP's reliability overhead is unacceptable. By implementing reliable messaging, ordering, and congestion control yourself — as QUIC does — you get the best of both worlds.
Read next: C23 Modern Evolution: auto, nullptr, constexpr →
Part of the C Mastery Course — 30 modules from C basics to expert systems engineering.
