December 5, 2026

TCP vs UDP Explained: Five Messages, Two Protocols and What Arrived

Two server racks with different cable arrangements in a dim room

A program sent five short messages, “msg1” through “msg5,” to another machine over UDP, and the receiver read back exactly five messages. Sent over TCP, the same five messages came out of the receiving socket as a single read: “msg1msg2msg3msg4msg5.” Nothing was lost or damaged. TCP simply never promised that the edges between messages would survive, and that one difference explains a surprising amount of how networked software is written. The tests below ran between two Linux network namespaces joined by a link with 20 ms of round-trip delay and adjustable packet loss, with tcpdump recording what went over the wire.

Video: Practical Networking on YouTube

Two contracts

TCP: a reliable byte stream

RFC 9293, the August 2022 standard that replaced the original 1981 TCP specification, describes TCP as providing a “reliable, in-order, byte-stream service.” Every byte arrives, in the order it was sent, or the connection reports an error.

UDP: individual datagrams

RFC 768, written by Jon Postel in August 1980, defines UDP in three pages. It calls the protocol “transaction oriented,” and states plainly that “delivery and duplicate protection are not guaranteed.” Each send is one datagram; it arrives whole, or not at all.

Neither is “better”

TCP trades time for certainty. UDP trades certainty for directness, and leaves anything else (retries, ordering, pacing) to the application, if it wants them.

Three white mail envelopes

Message boundaries

What the receiver saw

The first test sent five 4-byte messages each way. The UDP receiver’s reads came back as ['msg1', 'msg2', 'msg3', 'msg4', 'msg5']. The TCP receiver, which waited 0.3 seconds before reading, got ['msg1msg2msg3msg4msg5'].

Why TCP merges them

RFC 9293 says it directly: “TCP guarantees no correlation between the boundaries of TCP segments sent and received.” The application’s writes go into a stream, and reads take whatever bytes are waiting. The packet capture showed the sender itself combined them: msg1 went out alone, and the other four left together in one 16-byte segment. That’s Nagle’s algorithm at work, which the RFC describes as holding back small writes while earlier data is unacknowledged, so they can travel in fewer packets.

What programs do about it

Every protocol built on TCP needs its own way to mark where one message ends: a length prefix, a delimiter like a newline, or a fixed size. HTTP uses headers and lengths, and DNS over TCP, according to RFC 1035, prefixes each message with a two-byte length field. A program that assumes one send() equals one recv() works on a fast local network and fails intermittently everywhere else.

The cost of a connection

The handshake

TCP can’t send data until both ends agree on starting sequence numbers. RFC 9293 calls this the three-way handshake: SYN from the client, SYN-ACK from the server, then ACK. In the capture, three packets crossed the link before the first byte of data, and Python’s connect() call took 20.7 ms, one full round trip on a 20 ms link.

UDP just sends

The UDP sender had no setup at all. Its first datagram left immediately, carrying data. For a request that fits in one packet, such as a DNS lookup, that’s the difference between one round trip and two. RFC 1035 calls UDP “the recommended method for standard queries” for DNS, with UDP messages limited to 512 bytes in the original design.

Headers on the wire

tcpdump showed the overhead per packet for the same 4-byte payload:

Protocol IP packet length Breakdown
UDP 32 bytes 20 IP + 8 UDP + 4 data
TCP 56 bytes 20 IP + 32 TCP + 4 data

UDP’s header is always 8 bytes: source port, destination port, length and checksum. TCP’s minimum is 20 bytes, and Linux added 12 more here for timestamps.

A network switch with ethernet cables connected

When packets get lost

UDP loses what’s lost

The lab’s packet forwarder could drop a set percentage of packets at random. Sending 2,000 numbered UDP datagrams of about 500 bytes:

Loss rate UDP datagrams received
0% 2,000 of 2,000
2% 1,950 of 2,000
10% 1,784 of 2,000

No errors, no warnings. The missing datagrams were simply absent, and the application would never know unless it numbered its messages, as this test did.

TCP delivers everything

The same 2,000 lines over TCP arrived complete and in order at every loss rate: 2,000 of 2,000, in sequence, each time. The lost packets were detected and sent again, invisibly to the program.

The price is speed

Reliability costs throughput, and how much depends heavily on the sender’s congestion control algorithm. iperf3 ran 8-second TCP transfers over the 100 Mbit/s, 20 ms link with two different Linux algorithms:

Loss rate CUBIC BBR
0% 95.2 Mbit/s 95.4 Mbit/s
1% 5.9 Mbit/s 93.7 Mbit/s
5% 1.7 Mbit/s 88.9 Mbit/s

CUBIC, a long-standing default, treats every loss as a sign the network is congested and slows down, collapsing to 6 percent of its clean-link speed at 1 percent random loss. BBR, which Google engineers described in ACM Queue in 2016, instead models the path’s bandwidth and round-trip time, and kept nearly full speed. The article argues loss-based control misreads random loss as congestion and reports BBR reaching full throughput up to 5 percent loss on its test path.

Where each one is used

TCP: when every byte matters

Web pages over HTTP/1.1 and HTTP/2, email, SSH, file transfers and database connections all run on TCP. A web page missing a chunk of JavaScript is broken; a few extra milliseconds of waiting is not.

UDP: when late equals useless

Voice calls, video conferencing and online games typically use UDP. A voice packet that arrives 300 ms late after a retransmission is worse than no packet, because the conversation has moved on. These applications accept small gaps and conceal them.

DNS uses both

DNS shows how one service can use both protocols. Under RFC 1035, a UDP answer too large for the 512-byte limit is cut short and flagged with the TC (truncated) bit in its header. A resolver that sees the flag asks again over TCP, where the length-prefixed messages can be as large as needed. Zone transfers between DNS servers always use TCP for the same reason.

Real-time apps add their own rules

Many UDP applications number their packets and keep timestamps so they can reorder, skip or smooth over gaps, just as the numbered-datagram test did to count losses.

White and orange game controller

Knocking on a closed port

TCP gets a clear answer

A TCP connection attempt to a port where nothing was listening failed with ConnectionRefusedError after 20.6 ms, one round trip. The receiving machine’s kernel answered the SYN with a reset, and the sender knew immediately that nobody was home.

UDP gets silence, mostly

The same test over UDP behaved differently. A plain sendto() to the closed port returned no error at all: the datagram left, and the program had no idea whether anything received it. The receiving kernel did send back an ICMP “port unreachable” message, but an unconnected UDP socket doesn’t report it. A UDP socket that had been “connected” to that address with connect() did receive the news, though only on its next operation: the second send() raised ConnectionRefusedError.

Why scanners and firewalls care

That asymmetry is why checking whether a UDP service is running is slower and less certain than checking a TCP one. Silence might mean the service is up and simply didn’t reply, a firewall dropped the packet, or the ICMP reply was filtered on the way back. TCP’s reset is a definite no.

Seeing what’s listening

On Linux, ss -tuln lists listening sockets for both protocols, with -t for TCP, -u for UDP, -l for listeners and -n for numeric ports. In the lab’s receiving namespace, it showed a single line, iperf3’s server waiting on TCP port 5201 the default port iperf3 uses.

QUIC blurs the line

Reliable streams over UDP

The newest major transport sits on top of UDP. RFC 9000, published in May 2021, defines QUIC, which offers applications “flow-controlled streams for structured communication, low-latency connection establishment, and network path migration.” It is what HTTP/3 runs on.

Why build on UDP at all

The RFC gives the reason in one line: “QUIC packets are carried in UDP datagrams to better facilitate deployment in existing systems and networks.” Firewalls, routers and operating systems already pass UDP, so a new protocol riding inside it can be deployed in applications without waiting for every network device to learn a new protocol number.

What that means for the choice

QUIC adds back the reliability, ordering and congestion control that UDP lacks, while avoiding one of TCP’s weaknesses: a lost packet in one stream doesn’t stall the others. The real question is rarely “TCP or UDP” anymore. It’s which guarantees an application needs, and which layer should provide them.

Sources

  • RFC 9293, Transmission Control Protocol (TCP) (August 2022), RFC Editor: https://www.rfc-editor.org/rfc/rfc9293
  • RFC 768, User Datagram Protocol (August 1980), RFC Editor: https://www.rfc-editor.org/rfc/rfc768
  • RFC 1035, Domain Names – Implementation and Specification, RFC Editor: https://www.rfc-editor.org/rfc/rfc1035
  • BBR: Congestion-Based Congestion Control, ACM Queue (2016): https://queue.acm.org/detail.cfm?id=3022184
  • RFC 9000, QUIC: A UDP-Based Multiplexed and Secure Transport (May 2021), RFC Editor: https://www.rfc-editor.org/rfc/rfc9000

920 Posts

Peter Kalafatis writes and edits X9M8. He holds a bachelor's degree from LIU Post and a master's in Media Studies from The New School, with academic work in the philosophy of science, human behavior, AI, and memetics.

View All Posts

Leave a Reply

Your email address will not be published. Required fields are marked *