Building on UDP: Datagram Sizes, Server Design, UDP-Lite and UDP in Today's Internet
The practical side of UDP: the largest datagram you can send, what happens when the read buffer is too small, how a UDP server's bindings decide which socket gets a datagram, SO_REUSEPORT, dual-stack sockets, receive-buffer drops (measured), GSO, UDP-Lite, translation and today's UDP traffic.
kvi5hnuCode Sprout team member20 min readOn this page
The last three articles covered UDP's header and what IP does with datagrams that don't fit. This one is about writing and running software on UDP: how big a datagram can be, how a receiving program reads it, how the kernel decides which socket gets each datagram, and what UDP's lack of flow control means for a busy server. All the behavior shown is from Linux in the course lab. Other systems differ in places, and those differences are noted.
How big can a UDP datagram be?
The IPv4 Total Length field is 16 bits, so an IPv4 datagram is at most 65,535 bytes. Take away 20 bytes of IPv4 header and 8 of UDP header and you get 65,507 bytes of data. IPv6's Payload Length is also 16 bits, but it doesn't count the 40-byte IPv6 header, so UDP over IPv6 can carry 65,527 bytes (65,535 − 8). Larger IPv6 packets, called jumbograms (RFC 2675), exist only on special links and set the UDP Length field to 0.
Testing the limits in the lab, after raising the socket's send buffer so it wasn't the limiting factor:
IPv4: sendto() of 65507 bytes: ok
IPv4: sendto() of 65508 bytes: [Errno 90] Message too long
IPv6: sendto() of 65527 bytes: ok
IPv6: sendto() of 65528 bytes: [Errno 90] Message too long
That a datagram can be sent doesn't mean it will arrive. A 65,507-byte datagram becomes 45 fragments on Ethernet, and article 46 measured how badly that fails with even a little loss. Other limits get in the way first:
- Socket buffers. The send and receive buffer sizes (
SO_SNDBUF,SO_RCVBUF) cap the largest datagram a socket can handle. Linux defaults to about 208 KiB, but some systems default to much less. - Reassembly guarantees. IPv4 hosts only have to reassemble datagrams up to 576 bytes, and IPv6 hosts up to 1,500 bytes. Anything larger works only if the receiver happens to accept it. This is why classic DNS limited UDP messages to 512 bytes, and why DHCP messages are small.
- The path MTU. To avoid fragmentation altogether, stay within the path MTU minus headers (article 47). If you can't discover it, 1,200 bytes of UDP data is the conventional safe size, the one QUIC uses.
Reading a datagram into a buffer that's too small
A UDP read returns one datagram per call. The application says how many bytes it's willing to accept. What if the datagram is bigger? The server in the lab read three 1,000-byte datagrams (all As, then Bs, then Cs) with small buffers:
recv(100) returned 100 bytes
recvmsg(100) returned 100 bytes, MSG_TRUNC set: True
recv(100, MSG_PEEK|MSG_TRUNC) says the next datagram is 1000 bytes
recv(2000) returned 1000 bytes: b'CCCCCCCCCC'...
On Linux:
recv(100)returned the first 100 bytes of theAdatagram. The other 900 bytes were discarded, not saved for the next read. The next call started on theBdatagram.recvmsg()reports the loss: theMSG_TRUNCflag is set in the returned flags.- Passing
MSG_TRUNCas an input flag makes Linux return the datagram's real length instead of the number of bytes copied. Combined withMSG_PEEK, which reads without removing, a program can check the size of the next datagram before allocating a buffer for it. - The last read got the whole
Cdatagram, because 2,000 bytes was enough.
Other systems behaved differently in the past: some old System V systems returned the rest of the datagram in later reads, which silently merged datagrams. Today, POSIX systems (Linux, macOS and other Unix-like systems that follow the POSIX standard) and Windows truncate, and Windows reports it as the error WSAEMSGSIZE. The safe rule everywhere is to read UDP with a buffer at least as large as the biggest datagram your protocol allows, and check for truncation.
Which socket gets the datagram?
A UDP server's socket is bound to a local address and port, and optionally connected to a remote address and port. When a datagram arrives, the kernel picks the socket whose binding matches most specifically. The three kinds of binding, from most to least specific:
| Local address | Remote address | Receives |
|---|---|---|
| specific address : port | specific address : port (connected) | only from that one peer |
| specific address : port | any | datagrams sent to that one local address |
wildcard (0.0.0.0 or ::) : port | any | datagrams to any local address not claimed by a more specific socket |

In the lab, the server host had three addresses: 198.51.100.10, .11 and .12. A program opened three sockets on port 8888: one bound to .11, one to .12 and one to the wildcard. Binding several sockets to the same port this way needs the SO_REUSEADDR option. ss lists them:
State Recv-Q Send-Q Local Address:Port Peer Address:Port
UNCONN 0 0 0.0.0.0:8888 0.0.0.0:*
UNCONN 0 0 198.51.100.12:8888 0.0.0.0:*
UNCONN 0 0 198.51.100.11:8888 0.0.0.0:*
UNCONN is how ss shows an unconnected UDP socket, Recv-Q and Send-Q are bytes waiting in its buffers, and a peer of 0.0.0.0:* means it accepts datagrams from anyone. The client sent one datagram to each address, and a program on the server itself sent one to 127.0.0.1:
socket bound to 0.0.0.0:8888 got 'to 198.51.100.10'
socket bound to 198.51.100.11:8888 got 'to 198.51.100.11'
socket bound to 198.51.100.12:8888 got 'to 198.51.100.12'
socket bound to 0.0.0.0:8888 got 'to 127.0.0.1'
The specific sockets got the datagrams for their own addresses, and the wildcard socket got everything else, including the one to the loopback address. Without SO_REUSEADDR, binding the same port again fails:
bind 0.0.0.0:8888 without SO_REUSEADDR: [Errno 98] Address already in use
Learning which address a datagram was sent to
A server bound to the wildcard often needs to know which of its addresses the client used, for example to reply from that address, or to answer differently per address, as some DNS servers do. recvfrom() returns only the sender. The IP_PKTINFO socket option (IPV6_RECVPKTINFO for IPv6) adds the destination address to each datagram as ancillary data, extra control information that recvmsg() returns alongside the datagram. The client sent to two of the server's addresses, and a host on the server's LAN sent to the subnet broadcast address:
from 192.0.2.10: sent to 198.51.100.10
from 192.0.2.10: sent to 198.51.100.12
from 198.51.100.53: sent to 198.51.100.255
Only IP_PKTINFO lets a wildcard server tell the broadcast apart from a unicast request, which matters for a server that should answer broadcasts differently, or not at all.
Connected UDP sockets
connect() on a UDP socket sends nothing. It just records the peer, so the socket only accepts datagrams from that address and port, and the kernel picks the local address for it from the routing table. ss then shows it as ESTAB:
State Recv-Q Send-Q Local Address:Port Peer Address:Port
ESTAB 0 0 198.51.100.10:5555 192.0.2.10:4444
Some servers do this after hearing from a client, to talk to that client on a dedicated socket. Datagrams from anyone else don't match the connected socket. In the lab, no other socket was bound to port 5555, so the kernel answered the other host with ICMP Port Unreachable, which that host's own connected socket reported on its next send:
connected socket got b'from the chosen peer' from 192.0.2.10:4444
other host, second send: [Errno 111] Connection refused
Several servers on one port: SO_REUSEPORT
A single UDP socket is read by whichever thread calls recv(), and on a busy server one socket becomes a bottleneck. Linux's SO_REUSEPORT (since kernel 3.9) lets several sockets bind the same address and port, and spreads incoming unicast datagrams across them by hashing the source and destination addresses and ports. Each worker thread or process gets its own socket. In the lab, four sockets shared port 9100 and the client sent 1,000 datagrams, each from a new ephemeral port:
socket 1: 234 datagrams
socket 2: 267 datagrams
socket 3: 254 datagrams
socket 4: 245 datagrams
The split is roughly even, because each datagram came from a different source port and so hashed differently. Datagrams from the same source address and port always land on the same socket, which keeps a client's packets together. DNS servers, QUIC servers and load balancers use this. Every socket must set the option before binding, and on Linux all of them must belong to the same user.
Broadcast and multicast datagrams are different: a copy goes to every socket bound to a matching address and port (with SO_REUSEADDR), not just one.
On the BSDs and macOS, SO_REUSEPORT allows shared binding but doesn't load-balance in the same way. FreeBSD has SO_REUSEPORT_LB for that.
One socket for IPv4 and IPv6
An IPv6 socket bound to the wildcard :: can, on most systems, also receive IPv4 datagrams. They appear as IPv4-mapped IPv6 addresses, ::ffff: followed by the IPv4 address. Linux does this unless net.ipv6.bindv6only is 1 or the socket sets IPV6_V6ONLY:
net.ipv6.bindv6only = 0
IPv6 socket got b'over IPv4' from ::ffff:192.0.2.10
IPv6 socket got b'over IPv6' from 2001:db8:1::10
IPv4 bind after dual-stack IPv6 bind: [Errno 98] Address already in use
IPv4 bind after IPV6_V6ONLY bind: ok
A dual-stack socket also claims the IPv4 port, so a separate IPv4 socket can't bind it. With IPV6_V6ONLY set, the IPv6 socket handles only IPv6, and an IPv4 socket can share the port number. Windows sets IPV6_V6ONLY by default, and the BSDs default to v6-only too, so portable servers should set the option explicitly rather than rely on the default.
No flow control: the receive buffer overflows
Datagrams wait in the socket's receive buffer until the application reads them. UDP has no way to tell a sender to slow down, so if datagrams arrive faster than the program reads them, the buffer fills and the kernel drops new arrivals. Nobody is told: the sender doesn't know, and the application sees only missing data. Only a kernel counter, UdpRcvbufErrors, records it.
In the lab, a client sent a burst of 5,000 datagrams of 1,000 bytes to a deliberately slow reader, which paused 0.2 ms after each read. The test was repeated with different receive buffer sizes. Ordinary programs can't raise SO_RCVBUF above net.core.rmem_max, which was the default 208 KiB here, so the lab program used the privileged SO_RCVBUFFORCE to test larger sizes:
net.core.rmem_default = 212992
net.core.rmem_max = 212992
The results, with the size the program asked for, the size the kernel actually used (Linux doubles the request, to leave room for its bookkeeping), the datagrams delivered and the counter:
requested_rcvbuf kernel_rcvbuf delivered_of_5000 RcvbufErrors
16384 32768 211 4789
65536 131072 263 4737
262144 524288 414 4586
1048576 2097152 931 4069
4194304 8388608 3652 1348
16777216 33554432 5000 0

With small buffers, most of the burst was lost at the receiving host itself, after the network had delivered it perfectly. Each datagram uses more buffer space than its data, because the kernel counts its bookkeeping too, so even the 2 MiB buffer held fewer than a thousand 1,000-byte datagrams. Busy UDP servers such as DNS resolvers, syslog collectors and QUIC servers raise net.core.rmem_max and their socket buffers for this reason, and watch UdpRcvbufErrors (nstat -az UdpRcvbufErrors, or the receive buffer errors line in netstat -su).
The same problem exists inside the network. Router queues fill up when senders push more than a link can carry, and that is congestion. TCP slows down when it sees loss. UDP doesn't, so an application sending lots of UDP must implement congestion control itself, or keep its rate very low. RFC 8085, the IETF's guidelines for UDP applications, requires exactly that: bulk-transfer applications over UDP need congestion control equivalent to TCP's, and low-rate applications should send no more than about one datagram per round trip.
Sending many datagrams quickly: GSO
Each UDP send is a system call, and at high rates the per-call cost dominates. QUIC servers send millions of datagrams a second, so Linux added UDP generic segmentation offload (GSO): one sendmsg() with the UDP_SEGMENT socket option or ancillary data passes a large buffer and a segment size, and the kernel or network card splits it into many datagrams with identical headers. In the lab, one call sent 12,000 bytes with a segment size of 1,200:
one sendto() call with UDP_SEGMENT=1200 sent 12000 bytes
On the wire it appeared as ten ordinary 1,200-byte datagrams (the leading number is how many identical lines the capture had):
10 IP 192.0.2.10.46386 > 198.51.100.10.9002: UDP, length 1200
The receiving side has the matching UDP GRO (generic receive offload), which merges consecutive datagrams of one flow so that the application can read them in fewer calls. Together they're a large part of why QUIC can match TCP's efficiency on Linux.
UDP-Lite: checksum only part of the datagram
Some applications prefer damaged data to no data. A video codec, for example, can often display a frame with a few wrong bits, but UDP discards any datagram whose checksum fails. UDP-Lite (RFC 3828) addresses this with a partial checksum. It's a separate transport protocol with IP protocol number 136, and it reuses UDP's header with one change: the redundant Length field becomes Checksum Coverage, the number of bytes, counted from the start of the UDP-Lite header, that the checksum covers.

- Coverage 0 means the whole datagram is covered, like UDP.
- Values 1 to 7 are invalid, because the 8-byte header must always be covered.
- Any other value covers that many bytes. Errors after them don't cause a drop.
The lab's kernel (Linux 6.12) still supports it, through the IPPROTO_UDPLITE socket type and the UDPLITE_SEND_CSCOV and UDPLITE_RECV_CSCOV options. The client sent a 40-byte message covering 20 bytes (the header plus the first 12 data bytes), and the receiver accepted a coverage of at least 20:
UDP-Lite server got 40 bytes
ip.proto udp.srcport udp.dstport udp.checksum_coverage data.len
136 46434 9003 20 40
UDP-Lite never caught on. Most link layers discard frames with any bit error anyway, so damaged packets rarely reach UDP-Lite, and middleboxes often don't recognize protocol 136. Linux marked it deprecated in 2023 and removed it in Linux 7.1 (June 2026). Treat it as history; error-tolerant media today uses ordinary UDP with error concealment in the codec.
UDP through IPv4/IPv6 translators
When a NAT64 or stateless translator (article 38) converts UDP between IPv4 and IPv6, the checksum causes the only real complication:
- The pseudo-header changes, because the addresses change, so the translator must update the UDP checksum. Some address mappings are designed to be checksum-neutral, so the sum doesn't change.
- A UDP/IPv4 datagram with a zero checksum can't be sent as-is over IPv6, where zero is invalid. The translator must compute a checksum, which requires the whole datagram, or drop it. RFC 7915 lets administrators choose.
- A fragmented zero-checksum datagram can't be fixed by a stateless translator, which sees one fragment at a time, so it's dropped. A stateful NAT64 can reassemble the fragments first.
UDP in today's Internet
UDP's share of traffic has grown, mostly because of QUIC. Cloudflare's 2025 data shows about a fifth of the web requests it serves using HTTP/3, which runs over QUIC on UDP port 443. Other large users of UDP:
- DNS (port 53) for almost all name lookups (articles 49 to 56).
- Real-time media: RTP for voice and video calls, including WebRTC in browsers, plus games and live streaming.
- VPNs and tunnels: WireGuard, IPsec with NAT traversal (port 4500), OpenVPN, VXLAN and Geneve in data centers.
- Network services: DHCP (67/68), NTP (Network Time Protocol, 123), SNMP (Simple Network Management Protocol, 161), syslog (514), mDNS (5353).
Because UDP has no handshake, any host can send a datagram with a forged source address. That makes UDP services the tools of reflection and amplification attacks: the attacker sends small requests with the victim's address as the source, and the servers send much bigger replies to the victim. Open DNS resolvers, NTP servers with the old monlist command, SSDP, CLDAP (connectionless LDAP, used to query Windows directory servers) and memcached (which in 2018 amplified traffic by tens of thousands of times) have all been abused. Well-designed UDP protocols avoid sending a reply bigger than the request until the client has proved its address, which is why QUIC limits a server to three times the data received until the client's address is validated.
A practical issue for long-lived UDP flows is NAT timeouts. A NAT or firewall keeps a UDP mapping only while traffic flows, often for as little as 30 seconds, even though RFC 4787 asks for at least 2 minutes. Applications that must stay reachable send keepalives, and RFC 8085 recommends an interval no shorter than 15 seconds.
Common questions
Should I use UDP or TCP for my application?
Use TCP, or QUIC through a library, unless you have a specific reason not to. UDP is right when you need multicast or broadcast, when stale data is worse than lost data (live media, games), for small request-response exchanges like DNS, or when you're implementing your own transport, as QUIC does. If you build on raw UDP, you take on retransmission, ordering if needed, congestion control and path MTU handling yourself.
Why did my UDP server lose packets when the network wasn't busy?
Most likely the socket receive buffer overflowed because the application read too slowly, as in the lab. Check UdpRcvbufErrors with nstat or netstat -su. Raise SO_RCVBUF (and net.core.rmem_max on Linux), read faster, or spread the load over several sockets with SO_REUSEPORT.
What's the difference between SO_REUSEADDR and SO_REUSEPORT for UDP?
On Linux, SO_REUSEADDR lets sockets share a port when their local addresses differ, or when they receive multicast or broadcast, which every matching socket gets a copy of. SO_REUSEPORT lets several sockets bind exactly the same address and port and load-balances unicast datagrams among them. The details differ between operating systems, so check yours.
Can two programs on the same machine receive the same UDP datagram?
For unicast, no: one socket gets each datagram. For broadcast and multicast, yes: every socket bound to the matching address and port with SO_REUSEADDR gets its own copy. That's how several programs can listen for the same mDNS or multicast stream.
Does connect() on a UDP socket make it reliable?
No. It sends no packets and adds no reliability. It only fixes the peer address, so you can use send() and recv(), filters out datagrams from other peers, and lets the kernel report ICMP errors such as Port Unreachable to the socket.
What's the safest datagram size for an application that must work everywhere?
About 1,200 bytes of UDP data. It fits within IPv6's 1,280-byte minimum MTU with headers to spare, and within nearly every IPv4 path, including tunnels. QUIC uses this as its starting size. For larger messages, either implement path MTU discovery or split them yourself.
Why don't UDP servers have a listen() and accept() step?
Because there are no connections. A UDP server binds a socket and reads datagrams from whoever sends them, replying to the address each one came from. One socket serves all clients. If a server wants per-client sockets, it creates and connects them itself.
Summary
- The largest UDP payload is 65,507 bytes over IPv4 and 65,527 over IPv6 (without jumbograms), but large datagrams fragment and are fragile. Hosts are only required to reassemble 576 bytes (IPv4) or 1,500 (IPv6). About 1,200 bytes is the safe size.
- A UDP read returns one datagram. On Linux, an undersized buffer truncates it and discards the rest;
MSG_TRUNCreports or measures the real length. - The kernel delivers a datagram to the most specific matching socket: connected, then specific local address, then wildcard.
SO_REUSEADDRallows overlapping bindings,IP_PKTINFOreveals the destination address, andconnect()restricts a socket to one peer. SO_REUSEPORTspreads unicast datagrams across several sockets on one port by hashing addresses and ports. Broadcast and multicast go to every matching socket.- Dual-stack IPv6 sockets receive IPv4 as
::ffff:addresses unlessIPV6_V6ONLYis set; defaults differ by OS. - UDP has no flow control: a slow reader loses datagrams at its own receive buffer, counted in
UdpRcvbufErrors. It also has no congestion control, so applications must add their own (RFC 8085). - UDP GSO and GRO let one system call send or receive many datagrams, which QUIC depends on.
- UDP-Lite (protocol 136) checksums only part of the datagram. It saw little use and was removed in Linux 7.1.
- Translators must fix UDP checksums and can't handle zero-checksum fragments statelessly. UDP traffic is growing with QUIC, and its lack of a handshake makes it a vector for amplification attacks.
Practice questions
1. A program sends a 2,000-byte UDP datagram, and the receiver calls recv(buf, 1500). On Linux, what does it get, and what happens to the rest?
Answer
It gets the first 1,500 bytes. The remaining 500 bytes are discarded; the next recv() returns the next datagram. With recvmsg() the program can see the MSG_TRUNC flag, and with MSG_PEEK | MSG_TRUNC it can learn the full size before reading.
2. A host has addresses 198.51.100.10 and 198.51.100.11. One socket is bound to 198.51.100.11 and another to 0.0.0.0. Which socket receives a datagram sent to 198.51.100.10? And one sent to 198.51.100.11?
Answer
198.51.100.10 goes to the wildcard socket, because no specific socket claims that address. 198.51.100.11 goes to the socket bound to 198.51.100.11, because a specific local address beats the wildcard.
3. A syslog collector receives bursts of 20,000 messages per second, and some messages go missing even though the network shows no loss. What would you check, and what could fix it?
Answer
Check UdpRcvbufErrors (nstat -az UdpRcvbufErrors or netstat -su). If it's increasing, the socket receive buffer overflows during bursts. Raise net.core.rmem_max and the socket's SO_RCVBUF, make the reader faster, or use several sockets with SO_REUSEPORT and several reader threads.
4. Why does a QUIC server use SO_REUSEPORT with several sockets, and why doesn't that mix up packets from different connections?
Answer
It spreads the receive work across several threads or CPU cores, each with its own socket. The kernel picks the socket by hashing the source and destination addresses and ports, so all datagrams of one client flow (same addresses and ports) land on the same socket. A client that changes its address, for example during QUIC connection migration, needs extra steering, which QUIC servers handle with connection IDs and eBPF programs (small programs loaded into the Linux kernel that can choose the socket).
5. A dual-stack server on Linux binds an IPv6 socket to [::]:8080, and then a second program fails to bind an IPv4 socket to 0.0.0.0:8080. Why, and how can both coexist?
Answer
With IPV6_V6ONLY off (Linux's default), the IPv6 wildcard socket also receives IPv4 datagrams, so it claims the IPv4 port too. Set IPV6_V6ONLY on the IPv6 socket, or net.ipv6.bindv6only=1, so it handles only IPv6. Then the IPv4 socket can bind the same port number.
6. Why can't a stateless IPv4-to-IPv6 translator forward a fragmented UDP/IPv4 datagram whose checksum is 0?
Answer
UDP over IPv6 requires a valid checksum, so the translator must compute one, and the checksum covers the whole datagram. A stateless translator handles each fragment separately and never sees the whole datagram, so it can't compute the checksum and must drop the fragments.
7. An online game sends 60 small UDP updates per second over a home NAT. Why might the server's packets stop reaching a player who's idle for a few minutes, and how do games prevent it?
Answer
If the player's client stops sending, the NAT's UDP mapping times out (sometimes in 30 seconds to a few minutes), and the server's packets no longer match any mapping, so the NAT drops them. Games send periodic keepalive packets from the client, at intervals shorter than the NAT timeout but not shorter than about 15 seconds, as RFC 8085 recommends.
Review quiz
Test yourself on all of Part 8 with the UDP and Fragmentation review quiz: twenty questions on the UDP header and checksum, fragmentation and reassembly, path MTU discovery and black holes, and UDP server design.
Previous: Path MTU Discovery: Classic PMTUD, PLPMTUD and MTU Black Holes
Next: The DNS Name Space: Names, Labels, Zones and Delegation

More in TCP/IP Course
Quiz: TCP/IP Delicate Details
Twenty-five questions on the details that catch out experienced engineers: sequence numbers for SYN and FIN, TIME_WAIT versus tcp_fin_timeout, Karn's algorithm, socket buffer doubling, even and odd ephemeral ports, IPv6 checksums and fragmentation, negative DNS caching, and capture filter traps.
Quiz: TCP/IP Interview Questions
Thirty-three networking questions of the kind asked in developer, SRE and network engineering interviews: what happens when you load a URL, TCP versus UDP, handshakes, closing and TIME_WAIT, subnetting, NAT, DNS, MTU problems, congestion, TLS and QUIC, and debugging slow or failing connections.
Quiz: TCP/IP Course Final Exam (All Parts)
The final exam for the TCP/IP course: thirty questions across all fourteen parts, from packets and addressing through Ethernet, ARP, IP, DHCP, NAT, ICMP, UDP, DNS, TCP connections, reliability, congestion control, security, QUIC and troubleshooting.
Quiz: Modern Transports and Troubleshooting (TCP/IP Part 14)
Review modern transports and troubleshooting: QUIC packets, handshakes, 0-RTT, streams and connection migration, HTTP/3, Multipath TCP, SCTP multihoming, and a layered method for diagnosing real network faults.
Discussion
Sign in to ask a question or share what you learned.