131 TCP Services Deep Dive
131 TCP Services Deep Dive
TCP is a byte stream, not a message protocol. Most production bugs in custom TCP systems come from this single misunderstanding.
Core Mental Model
application message A+B+C
|
v
TCP stream bytes: [A-part][A-rest+B][C-part]...
Your code must define message boundaries explicitly.
Framing Strategy
A robust approach is length-prefix framing:
[4-byte length][payload bytes]
This allows the receiver to know exactly how many bytes belong to one logical message.
Connection Lifecycle
accept -> set deadlines -> read frame -> validate -> handle -> write response -> close/reuse
Each stage needs failure policy. For example, malformed frames should be rejected quickly, while slow clients should hit read deadlines instead of consuming handlers forever.
Production Concerns
- Deadlines: Every read/write path should have bounded time.
- Memory: Never allocate unbounded payload buffers.
- Concurrency: Limit active handlers to avoid overload collapse.
- Observability: Record accepts, active conns, frame errors, and timeouts.
Design Notes
When throughput matters, separate IO and business logic pools so slow CPU work does not block socket reading. When reliability matters, prioritize predictable timeouts and explicit protocol errors over maximum raw throughput.