TCP Has No Message Boundaries: Why Framing (and PacketRule) Matters

ayumaxPluginsObjectDeliverer

TCP Has No Message Boundaries: Why Framing (and PacketRule) Matters

You send two messages from your Unreal Engine app to an external tool. The tool receives one long blob. Or it receives half of the first message, and the rest shows up a moment later glued to the second one. Nothing is lost and nothing is out of order — yet the data looks broken.

This is not a bug in your code, and it is not a bug in TCP. It is what TCP is: a byte stream. If you are building anything that exchanges messages over TCP, this is the first thing to understand, because every later design decision — including why libraries like ObjectDeliverer ask you to pick something called a PacketRule — follows from it.

TCP moves bytes, not messages

When your code sends “ABC” and then “DEF”, TCP does not promise that the other side reads “ABC” and then “DEF”. It promises that the bytes arrive in order and without loss. How they are grouped when your receiving code reads them is up to the operating system, the network, and timing.

Figure 1: Two sends over TCP can be read as one merged chunk or split across reads

So a receiver might read both messages at once as “ABCDEF”, or read “AB” first and “CDEF” on the next read. Both are correct behavior. The boundary between your two messages existed only in your head — TCP never knew about it.

UDP does not have this problem, because a UDP datagram is already a message: what you send as one packet is received as one packet. (Choosing between the two is covered in TCP vs UDP: How to Choose.) With TCP, the boundary has to be added by you.

The fix is an agreement: framing

The standard fix is called framing: before sending, you reshape each message so the receiver can find where it ends. There are only a few basic shapes, and you have probably seen all of them:

  • Size header — write the body length first, then the body. The receiver reads the length, then reads exactly that many bytes.
  • Delimiter — end each message with a marker that never appears inside the body, such as a newline. Text protocols and many devices work this way.
  • Fixed length — agree that every message is exactly N bytes. Simple, and common for sensor frames that always carry the same fields.
Figure 2: Framing with a size header

Two points matter more than the shape itself. First, both ends must use the same rule. If the sender frames with a size header and the receiver expects newlines, you get garbage — usually without any error message, which makes this a particularly confusing class of bug. Second, when the other end already exists — a device, a server, another team’s program — its format is already decided. Your job is to match it, not to choose freely.

Why ObjectDeliverer asks you to choose a PacketRule

If you use ObjectDeliverer, starting communication means passing a Protocol and a PacketRule to Start. Users sometimes ask why the PacketRule step exists at all — why the plugin does not just handle it.

Now you know the answer: the PacketRule is the framing agreement, and there is no universal default that would be right for everyone. A rule that is wrong for the program on the other end does not fail politely; it silently produces merged or split messages. So the plugin makes the choice explicit instead of hiding it.

ObjectDeliverer ships four rules, which map directly onto the framing shapes above:

PacketRule Framing Fits when
SizeBody Size header (1, 2, 4, or 8 bytes, big or little endian) You design both ends, or the other end already uses a size header
Terminate Delimiter (newline CRLF by default) The other end speaks in lines of text
FixedLength Every message is the same size Fixed-size frames, such as periodic sensor data
Nodivision No framing; each received packet is one message Protocols where one packet already is one message, such as UDP

Details for each rule are in the documentation’s packet division rules section.

How to pick one

A short decision guide:

  1. The other end already exists. Match its format. Lines of text point to Terminate. Fixed-size binary frames point to FixedLength. A documented size header points to SizeBody, with the same header size and endianness.
  2. You are designing both ends. SizeBody is a good default. Message sizes can vary freely, and the receiver’s logic stays simple: read the header, then read that many bytes.
  3. You are not on TCP. If each packet is already a complete message, as with UDP datagrams, Nodivision is the honest choice — there is nothing to frame.

And whatever you pick, pick the same rule on both ends. When messages arrive merged, split, or shifted by a few bytes, check the framing agreement before you check anything else. In our experience, that is where this class of bug almost always lives.

Framing feels like a small ceremony when you first meet it. It is actually the moment you write down where your messages end — and once it is written down, TCP’s stream stops being a source of surprises and becomes what it should be: just a pipe.

Follow @AyumaxSoft on X to get the latest updates from AyumaxSoft as soon as they are available.

Related Posts