NOTE

Packet Protocol

What sticky packets and split packets are, why the phenomenon appears with TCP byte streams, and how application-layer framing solves it.

JavaCreated Updated 1 min readhistorical

This is a historical learning note and may contain outdated or incomplete understanding.

1. What Are Sticky Packets and Split Packets?

  1. Actual data
Hello World
Hello World
  1. Sticky-packet phenomenon
Hello World Hello// The second packet is merged into the first packet
World
  1. Split-packet phenomenon
Hello
World Hello World// The first packet is split into the second packet

2. Why Does It Happen?

TCP has this issue because it is a byte-stream-oriented protocol and cannot tell which range of bytes forms a complete message.

UDP does not have this issue because it is message-oriented and preserves boundaries between datagrams.

Strictly speaking, “sticky packets” do not actually exist. The term “sticky packet” is mainly used for readers with limited or less systematic network-programming experience.

TCP does not have the concept of application packets; it is a stream.

Since TCP itself has no packet concept, how can there be “sticky packets”? Obviously, the concept of a packet appears at the application layer (in the five-layer TCP/IP network model). The essence of a “sticky packet” problem is that information is extracted incorrectly from the stream, making two pieces of information appear as if they were stuck together.

In fact, this is a parsing error. Its essence is incorrect parsing of binary data. It is not fundamentally a TCP problem; it means the application-layer information parser cannot correctly process the data read from the socket.

3. How to Handle It

  • Define a custom protocol.

3.1. Delimiter

Read byte by byte and determine whether the current byte is the delimiter.

3.2. Length + Data

* Packet format
* +----------+----------+----------+----------+----------+
* | Header   | Module   | Command  | Length   | Data     |
* +----------+----------+----------+----------+----------+
* Header: 4 bytes
* Module number: 2-byte short
* Command number: 2-byte short
* Length: 4 bytes (describes the byte length of the data section)

4. References

Discussion

Sign in with GitHub to comment. Discussions are stored as GitHub Issues.View on GitHub