KMC
KMC

Reputation: 20046

What is a message boundary?

What are "message boundaries" in the following context?

One difference between TCP and UDP is that UDP preserves message boundaries.

I understand the difference between TCP and UDP, but I'm unsure about the definition of "message boundaries".

Since UDP includes the destination and port information in each individual packet, could it be this that gives message a "boundary"?

Upvotes: 32

Views: 19991

Answers (3)

Preston L. Bannister
Preston L. Bannister

Reputation: 383

You should be careful to not confuse application-level and protocol-level message/packet boundaries. They are very different things. The question does not clearly distinguish between very different concepts.

As a cheat, on an isolated subnet, with small messages, when reliable and ordered delivery is not required - you can cheat and use UDP. A single UDP send/receive will always contain one message - which is simpler to code.

When reliable and ordered delivery is required, or larger application-level messages are needed - then you want to use TCP. Yes, a small bit of discipline is required of your application. You need to properly serialize/deserialize your application-level messages through the TCP stream. This is easily done.

The "answer" is that application-level message boundaries must be assured by the application. UDP can serve for some applications. TCP is better for others (and a much larger set).

Also, if you have multiple threads doing unmanaged writes to a single stream (network or file) then that is a problem in your application.

Upvotes: 0

aidanok
aidanok

Reputation: 859

Message boundaries in this context is simply the start & end of the message/packet. With TCP connections, all messages/packets are combined into a continuous stream of data, whereas with UDP the messages are given to you in their original form. They will have an exact size in bytes.

Upvotes: 3

David Schwartz
David Schwartz

Reputation: 182753

No, message boundaries have nothing to do with destinations or ports. A "message boundary" is the separation between two messages being sent over a protocol. UDP preserves message boundaries. If you send "FOO" and then "BAR" over UDP, the other end will receive two datagrams, one containing "FOO" and the other containing "BAR".

If you send "FOO" and then "BAR" over TCP, no message boundary is preserved. The other end might get "FOO" and then "BAR". Or it might get "FOOBAR". Or it might get "F" and then "OOB" and then "AR". TCP does not make any attempt to preserve application message boundaries -- it's just a stream of bytes in each direction.

Upvotes: 54

Related Questions