Mohamed Yacout
Mohamed Yacout

Reputation: 133

read messages on a streaming socket in c

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

Upvotes: 1

Views: 1582

Answers (2)

Robert Larsen
Robert Larsen

Reputation: 1119

There are many types of delimiters. Whatever character is "illegal" in your protocol will make an excellent delimiter, but if you can choose a printable character that will make the protocol more readable in a sniffer or when using telnet.

Basing your protocol on XML or similar structured data is also an option. This makes a more complex delimiter.

Binary protocols often use TLVs (http://en.wikipedia.org/wiki/Type-length-value) where the length specifies the number of bytes in the message (only the value part or both type, length and value).

Upvotes: 1

unwind
unwind

Reputation: 399703

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.

Upvotes: 3

Related Questions