Learning path

Full curriculum

Full curriculum

Unit content

Stream sockets, framing and partial I/O

A TCP socket exposes a byte stream, not a sequence of application messages. Therefore application message boundaries must be represented explicitly.

A call that writes 1000 bytes does not guarantee that the peer receives those bytes in one read. A read may return only part of a message, several messages together, or any available prefix of the stream.

Applications commonly frame messages with a fixed-size header, a length prefix, a delimiter, or a self-describing encoding. If a four-byte length says that 200 bytes follow, the receiver must continue reading until those 200 bytes have actually arrived.

Writes can also be partial, especially on nonblocking sockets, so unsent bytes must remain buffered for later transmission.

Correct stream protocols therefore treat read and write as movement of available bytes, not as preservation of application-level message calls.