Ch. 20 · Networking

TCP Byte Streams and Message Framing

TCP Byte Streams and Message Framing. Learn the reasoning, a practical example, common mistakes and an interview exercise.

~2 min readbeginnerupdated Oct 3, 2026

TCP delivers an ordered byte stream rather than preserving application write boundaries. Applications need a framing protocol.

Before you start

You should understand clients, servers, IP addresses and ports. Follow a request through name resolution, connection establishment and application exchange. Distinguish protocol guarantees from deployment policy, and use observations from the relevant layer rather than guessing from one browser error message.

The practical goal is to reason through this situation: One message can arrive across multiple reads, or multiple messages can arrive together. Read the walkthrough first, then try the interview exercise before opening its answer. The important part is explaining the decision and its consequences, rather than remembering a definition alone.

Step-by-step walkthrough

Step 1: Define frame boundaries

Application writes are not preserved as individual TCP messages.

Step 2: Parse incrementally

Keep incomplete input until enough bytes identify a full frame.

Step 3: Bound frame size

Reject malformed or oversized lengths before uncontrolled buffering.

Worked scenario

One message can arrive across multiple reads, or multiple messages can arrive together.

A sender writes HELLO and WORLD separately. The receiver can read HE, then LLOWORLD, or both messages together. A length prefix tells the parser when one frame completes; it must also handle a prefix split across reads and an incomplete body at disconnect.

Common mistake

Assuming one read equals one message breaks under ordinary network behavior.

Verify the behavior

Split and combine input at every boundary and test oversized or truncated frames.

Interview exercise

Design a small framed protocol.

Answer and reasoning

Use a length prefix or delimiter with limits and incremental parsing; handle incomplete and malformed frames.

Continue learning

Compare the scenario with the Networking interview questions and test your understanding with the Networking MCQs. For terminology and implementation details, consult the reference material.

More in Networking

read ✓Networking · mid

Networking: DNS Caching and TTL

Understand positive and negative caching, why changes are not instant, and how TTL trades propagation speed against query load.

~2 min readread →
read ✓Networking · easy

Networking: DNS Record Types

Read and choose DNS records: A and AAAA for addresses, CNAME for aliases, and MX and TXT for mail and verification.

~2 min readread →
read ✓Networking · mid

Networking: HTTP Redirects

Choose between 301, 302, 307 and 308, know which preserve the request method, and avoid caching and loop mistakes.

~2 min readread →
esc