7 routes42 sections324 units

Unit 3.4 · about 5 h

252 / 324

Sockets, partial reads and writes, and message framing

TCP hands you a byte stream; messages are your construction. This unit builds the framing layer the fault-tested service slice reuses.

  • implement

You can now

  • Implement a length-prefixed framed protocol that survives partial reads, partial writes and coalesced arrivals.
  • Derive framing state for partial, coalesced, oversized, truncated and malformed input.
  • Enforce size limits and version checks, and close cleanly on protocol errors.

Covers

  • Socket lifecycle, address resolution and blocking behavior
  • Partial reads, partial writes and coalesced messages
  • Length-prefixed framing, limits and malformed input

Study, in this order

5 steps

  1. Stream sockets client and server
    Beej's Guide to Network Programming - sockets to multiplexing

    Section 5.7 (send and recv); Sections 6.1 and 6.2 (simple stream server and client); build the pair first

  2. Partial sends and shutdown
    Beej's Guide to Network Programming - sockets to multiplexing

    Section 7.4 (Handling Partial sends); Section 5.9 (close and shutdown)

    Depth · Use shutdown for the half-close direction vocabulary only; full drain behavior belongs to the connections unit.

  3. Stream parsing and the word server
    Beej's Guide to Network Concepts - TCP and UDP path

    Section 11.2 (Processing a Stream into Packets); Section 11.3.1 (multiple sentences at once); Chapter 13 (The Word Server: get_next_word_packet and extract_word)

  4. Framed TCP sockets in Python
    AlgoMaster Networks - pcap and concurrent server labs

    Why One recv() or read() Is Not Enough; Python: Reading and Writing Framed Messages; Closing and End-of-Stream; Timeouts Bound Waiting

  5. A production-shaped echo protocol
    AlgoMaster Networks - pcap and concurrent server labs

    The Persistent Echo Protocol (4-byte big-endian length prefix, 64 KiB limit); Concurrency Boundaries; Admission Before Task Creation

    Depth · Adopt the length prefix, size limit and 30-second frame deadline. Leave the Java port and the load client for the latency unit.

Focus on

  • Every recv can return one byte, one frame, several frames, or anything between. The parser keeps explicit state: bytes still needed for the header, then bytes still needed for the payload.
  • sendall is a loop, not a guarantee. A raw send can write fewer bytes; the remainder must be retried, not dropped.
  • Limits come before parsing: check the declared length against the maximum before allocating or reading the payload.
  • A version or length violation is a protocol error. Reply if the protocol allows, then close. Never resynchronize by scanning payload bytes for a magic value.

Reproduce closed-book

With CPython 3.14.4 on loopback, send two frames of a 4-byte length prefix plus 10 bytes back-to-back (28 wire bytes), wait until both sends have landed, then read with recv(5): exactly 6 calls return all bytes. Repeat with one recv(4096): a single call returns all 28 bytes, both frames coalesced. Then feed your framer the same bytes split at every offset and check it yields the same two messages each time.

Traps

  • Assuming one send equals one recv. Coalescing and splitting are normal; the 28-byte two-frame burst routinely arrives in one recv on loopback.
  • Growing the buffer from the declared length without a cap. A corrupt 4 GB prefix becomes a memory-exhaustion attack; validate first.
  • Treating a clean FIN mid-frame as a complete message. A peer that began a frame and closed sent a truncated message: a protocol error, not a short message.

Practice

Beej's Network Concepts: Project The Word Server

Chapter 13: implement get_next_word_packet and extract_word against the word-packet format

A stream-to-packet parser with the same state shape as your length-prefixed framer.

Self-test

  1. recv(5) returned 3 bytes of a 4-byte header, then the next recv returns 0 bytes. What happened and what does the framer do?

  2. The length prefix says 100 bytes but only 60 arrived and there was no FIN. What does the framer do?

  3. Why check the length against 64 KiB before reading the payload?

  4. Two frames arrive in one recv. Does the application need two reads?

Completion criterion

Stop when

Your framer passes partial, coalesced, oversized, truncated and garbage-prefix cases, and a peer that closes mid-frame yields a protocol error, never a short message.

Mark passed, then open 3.5 Only mark this when you can do it without notes.

Leads to

Comes after

Resources for this unit

  • Beej's Guide to Network Programming - sockets to multiplexing
    Brian Hallprimarycourse
    Read What is a socket?, IP addresses and data structures, Client-server background, System calls or bust, Client-server examples, Slightly advanced techniques and sendall. Build the simple TCP pair first, then replace delimiter assumptions with a bounded length-prefixed protocol.
  • Beej's Guide to Network Concepts - TCP and UDP path
    Brian Hallrequiredtheory
    Read the OS and sockets, protocol layering, IP, UDP and TCP chapters alongside packet captures from your own client and server. Use it to explain transport behavior rather than memorize layer names.
  • AlgoMaster Networks - pcap and concurrent server labs
    AlgoMasterrequiredpractice
    Use Reading a Packet Capture: A Real Bug and Building a Concurrent TCP Echo Server after the Beej implementation. Reproduce one packet-level observation and one admission, deadline or shutdown behavior in your independently written service.

Also in this section

  • MIT 6.5840 - RPC, threads and fault vocabulary
    MIT PDOSsupporttheory
    Read the introductory RPC and threads material plus the lab guidance. Use it to enumerate timeout ambiguity and duplicate effects. Stop before Raft and the full lab sequence.
  • Replicated Log and Stream Processor - protocol slice
    Roadmap-Finalsupportproject
    Use only the protocol requirements, deterministic network fault matrix and delivery-contract questions. Build the single-node framed service now; keep replication and consensus for the later project.
  • Designing Data-Intensive Applications, 2nd edition - Chapter 5
    O'Reillyrequiredtheory
    Read Formats for Encoding Data (printed pp. 163 to 177) for schema merits and compatibility, and Dataflow Through Services: REST and RPC (printed pp. 180 to 186) for service dataflow, version coexistence and the idempotence pointer.
  • gRPC Deadlines guide
    gRPC Authorsreferencereference
    Read Overview, Deadlines on the Client and Deadline Propagation for absolute deadlines, remaining-time forwarding and clock-skew shielding.
  • gRPC Status Codes
    gRPC Authorsreferencereference
    Read the full status list, including DEADLINE_EXCEEDED, UNAVAILABLE and the codes never generated by the library, for the error taxonomy.
  • W3C Trace Context
    W3Creferencereference
    Read Section 3.2 for the traceparent format and Sections 3.4 to 3.5 for the legal traceparent and tracestate mutations when forwarding.

Quick find

Search Michi

Type to search every route, section, unit, topic and resource.

Enter opens the first result Esc closes

Reference

Keyboard shortcuts

n
Continue to the first unit not passed
j / k
Next / previous unit
1 2 3
Toggle learned, practised, passed
/
Open search
?
Show this help
Esc
Close open panels