Choosing a Stream or Datagram Transport
A byte stream and a datagram service expose different boundaries and delivery guarantees to application code.
- Source
- Anbloq
- Language
- English
- Created
- Updated
- Chapters
- 4
- Reading time
- 1 min
In this article4 chapters
Choose an example with ordered bytes
A file reader expecting every byte in order can suit a reliable stream interface. It still needs application record framing. See TCP byte streams.
Choose an example with independent samples
An application may use datagrams where each sample is useful independently and its rules tolerate loss or reordering. Document the required handling of missed samples. UDP datagrams describe which guarantees have to come from higher layers.[1][2]
Choose whether the application expects datagrams or an ordered stream
A sensor can send one observation per UDP datagram and tolerate missing samples. A file transfer can use TCP's ordered stream and application-level framing. A UDP receiver can observe message boundaries, but delivery, order and duplicate behavior need an application policy. See streams and packets.
Datagram design: one reading per received datagram
Stream design: header denotes length, payload can span reads
Both designs still need to define record interpretation
Test the behavior the chosen transport leaves to the application
Datagram loss and reordering can make adjacent readings discontinuous. A stream can close after partial data and prevent completion of a record. Check what the caller sees in both forms.