UDP, TCP and the byte stream
UDP sends datagrams and promises nothing. TCP promises an ordered, reliable stream of bytes, but not messages. Learn why every protocol on top of TCP must frame its own messages.
The two main transports make opposite choices. UDP sends independent datagrams: each send becomes one packet, it may arrive, arrive twice, arrive out of order or not at all, and nobody tells you. TCP gives you a connection and an ordered, reliable stream of bytes, retransmitting lost data and slowing down when the network is busy.
| UDP | TCP | |
|---|---|---|
| Unit | Datagram, boundaries preserved | Byte stream, no boundaries |
| Delivery | Best effort | Reliable and in order |
| Connection setup | None | Three-way handshake |
| Congestion control | None built in | Built in |
| Used by | DNS, video calls, games, QUIC | HTTP/1.1, HTTP/2, databases, SSH |
The sockets API
Programs talk to the network through sockets, which are file descriptors (Module 2). A server calls socket, bind to a port, listen, then accept for each client. A client calls socket then connect. After that, both sides read and write bytes. Everything a harness does over HTTP rests on these few calls.
No message boundaries
This is the most important property of TCP and the most common bug. If the sender writes hello and then world, the receiver may read helloworld in one call, or hel then loworld, or one byte at a time. TCP only promises that the bytes arrive in order. How they are grouped into reads depends on packet sizes, timing and buffers.
Three messages, random reads
- read 1
20:{"id": - read 2
1,"tool":"ls"}21:{"id":2," - read 3
tool":"cat"}22 - read 4
:{"id":3,"tool":"grep"}
- ✓ after read 2
{"id":1,"tool":"ls"} - ✓ after read 3
{"id":2,"tool":"cat"} - ✓ after read 4
{"id":3,"tool":"grep"}
All 3 messages recovered intact, whatever the read sizes.
Framing: three common strategies
- 01Delimiters
End each message with a byte that cannot appear inside it, such as a newline. JSON Lines and SSE (a blank line) work this way. Simple, but the delimiter must be escaped inside messages.
- 02Length prefixes
Send the message length first, then exactly that many bytes. gRPC uses a 5-byte prefix; many binary protocols use a 4-byte big-endian length.
- 03Self-delimiting formats
Some encodings reveal their own end, such as HTTP/1.1 headers ending in a blank line plus a
Content-Length.
/** Reads 4-byte big-endian length-prefixed messages from a TCP stream. */
export function frameReader(onMessage: (msg: Buffer) => void) {
let buffered = Buffer.alloc(0);
return (chunk: Buffer) => {
buffered = Buffer.concat([buffered, chunk]);
while (buffered.length >= 4) {
const length = buffered.readUInt32BE(0);
if (length > 16 * 1024 * 1024) throw new Error("frame too large"); // never trust the peer
if (buffered.length < 4 + length) break; // wait for more bytes
onMessage(buffered.subarray(4, 4 + length));
buffered = buffered.subarray(4 + length);
}
};
}
// socket.on("data", frameReader((msg) => handle(JSON.parse(msg.toString("utf8")))));The size check matters: without it, a hostile or buggy peer can claim a 4 GB message and make you buffer forever.
Go deeperWhy would anyone choose UDP?
Reliability has a price. When a TCP packet is lost, every later byte waits until the lost one is retransmitted, even bytes that have already arrived. For a live video call, a frame that arrives 300 ms late is worse than a missing frame. UDP lets the application decide what to do about loss.
UDP is also the escape hatch for new transports. Changing TCP requires kernel updates on billions of devices and middleboxes that understand the change. A protocol built on UDP can live in user space and ship with an app. That is exactly how QUIC, and therefore HTTP/3, was deployed (Module 5).
Where you will meet it
Log shippers read files and sockets as streams and split on newlines, keeping any partial last line for the next read.
Their wire protocols prefix values with lengths or type bytes so clients can read exact message boundaries from the TCP stream.
Model APIs send Server-Sent Events, framed by blank lines. Clients that parse each network chunk as one event drop or corrupt tokens under load.
Words to know
- Datagram
- A self-contained UDP message delivered whole or not at all.
- Socket
- A file descriptor for one end of a network connection.
- Framing
- How a protocol marks where one message ends and the next begins.
- Length prefix
- A fixed-size field stating how many bytes of message follow.
Check yourself
Answer out loud before you open each one.
The sender writes two 10-byte messages. How many reads will the receiver need?
Any number from 1 to 20. TCP preserves order, not boundaries.
Name three framing strategies.
Delimiters, length prefixes and self-delimiting formats.
Why check the length prefix against a maximum?
A peer can claim an enormous length and make the reader allocate or wait without limit.
Try it
Start nc -l 9000 in one terminal and nc localhost 9000 in another. Write a Node client that sends three messages in a loop with no delay, and log each data event on a Node server. Count events versus messages.