Questions / Quant development, Networking & latency

TCP does not preserve your message boundaries

Foundation, about 6 minutes. Question D021.

An application sends two messages using two TCP send calls. Can the receiver obtain them in a different number of receive calls?

  1. AYes; receive calls can split or combine the sent byte sequences.
  2. BYes; TCP may also deliver the two messages in the opposite order.
  3. CNo; ordered delivery maps each send call to one receive call.
  4. DNo; a large enough receive buffer preserves the send-call boundaries.

Answer it in the app It's free. You get hints, a second try if you miss, and a follow-up question.

Show the answer and worked solution

Answer: A. Yes; receive calls can split or combine the sent byte sequences.

  1. TCP exposes an ordered byte stream. Separate application sends do not create preserved application message boundaries.
  2. One receive may return part of a message or bytes spanning more than one message.
  3. The application needs an explicit framing protocol and must buffer partial frames.

Interview shortcut. TCP is a byte stream; sends do not map to receives.

Takeaway. Ordering and framing are distinct protocol properties.

Why the other choices fail

  • Yes; TCP may also deliver the two messages in the opposite order. TCP preserves byte order. The issue is that it has no message boundaries, so reads can split or merge them.
  • No; ordered delivery maps each send call to one receive call. TCP ordering preserves byte order, not the application’s grouping of bytes into separate send calls.
  • No; a large enough receive buffer preserves the send-call boundaries. A larger buffer changes capacity but does not turn the TCP stream into a message-boundary protocol.

In the app this has a follow-up where one thing about the setup changes. Try the follow-up.

More networking & latency questions