NOTE
Message Framing over TCP
Length-prefix, delimiter, and fixed-size framing for converting a TCP byte stream into application messages.
This is a historical learning note and may contain outdated or incomplete understanding.
TCP provides an ordered byte stream, not application message boundaries. One write can be split across reads, and several writes can appear in one read.
Common framing strategies are:
- fixed-size messages;
- delimiter-terminated frames with escaping/validation;
- fixed-size header containing body length;
- self-describing formats whose parser can determine complete-frame length.
A decoder must retain partial bytes across reads and process multiple frames from one buffer. Validate declared lengths before allocation and enforce maximum frame size to prevent memory/DoS problems.