Messaging protocol
How do I create a message log so that I differentiate between user data and message completion.
For example, I take data from a user to send it to a client over TCP / IP, now the client side needs to know the length of the message, what I thought I was using some kind of completion by which the client recognizes this message has the end been reached?
How do I like this in C ++. I am using windows sockets
source to share
A fairly general strategy for application specific protocols - instead of using termination - to encode the message length before the message. Thus, the first few bytes (how many) determine the length of the message, and some special code (usually all bits or all bits off) denotes an overlapped message that needs to be continued.
Edit: An example was requested. Let's say we designed our protocol so that the maximum message size is 255 bytes and we want to send the string "hello". Our protocol message consists of three bytes: 2
, 104
, 105
. The first byte 2
tells us the length of the message following it. The second and third bytes, 104
and 105
, are ASCII for h
and i
.
source to share
There are many computer systems that use special markers to determine the end of a message - c uses \ 0 for character arrays, JPEG uses 0xFF as a marker, etc.
All these systems lead us to the conclusion that message prefixes with their length are much simpler and more reliable.
TCP itself actually does this. If you write two bytes of user input to a socket, there will be two bytes on the other end, and the TCP packets being processed by the system know this because they tag each packet with their payload length.
After you have sent input, you can shutdown the socket on one side and the other side will be notified (if they "have enabled such notifications)."
source to share