I’m writing a client server model application, The server sends a lot of messages (with different sizes) on very high rates on a stream connection to the client, The client reads these messages on a buffer larger than the expected message size
while (ret = read (sd, buf, sizeof buf) > 0)
{
// decode message here
}
The problem here is that the client may read more than one message on the buffer like that, If I supposed the buffer is B and the messages are M#, It will receive the message as following
|—-B—-|—-B—-|—-B—-|—-B—-| ….
|-M1–|-M2-|–M3-|-M4-|—M5—|–M6-| ….
which makes the decoding of the message incorrect. So, I want to receive the messages as following (only one message per buffer)
|—-B—-|—-B—-|—-B—-|—-B—-| ….
|-M1–|,,|-M2-|,,,|–M3-|,,|-M4-| …. (,,, are nothing, just for formatting)
Noting that, I have to use streaming connection and I don’t like not use any delimiter between messages
You cannot control how the bytes of the stream are delivered to the application, it’s a stream of bytes, not of individual messages.
Even if you don’t “like” delimiters, this is exactly why they are being used so often. It’s not a fun idea, it’s a basic solution to this problem. Not “liking” it isn’t very interesting as objections go.
Your only hope, excepting delimiters, is to implement message parsing that can properly determine at all times if it has a full message or not, and if so which message. If the messages are “prefixing”; so that a particular sequence of bytes B can be both a full message M1 and the start of M2, then I think you’re toast.