Writing a stream protocol: Message size field or Message delimiter? -


I am about to write a message protocol on a TCP stream. The receiver should know where the message limits are

I can send either 1) Fixed length message, 2) Size field so that the receiver knows how big the message is or 3) a unique message terminator ( I think it can not be used anywhere else).

I will not use # 1 for efficiency reasons.

I like # 2, but is it possible to get out of sync? I do not like idea # 3 because it means that the receiver can not know the size of the message prematurely and it is also important that the Terminator does not appear anywhere in the message. With the # 2, if it is possible to get out of sync, can I add a terminator or are I guaranteed not to be out of sync when the sender program that sends is correct? Is it necessary to do # 2 and # 3?

Please tell me.

Thank you, jbu

You are using TCP, packet delivery is reliable If the connection is either drops, timeouts or you will read the entire message. So option # 2 is fine.


Comments

Popular posts from this blog

c++ - Linux and clipboard -

Visual Studio 2005: How to speed up builds when a VSMDI is open? -

booting ubuntu from usb using virtualbox -