I’ve experimented a bit with async TCP socket messages between two programs, for passing data, numbers and/or text. What I’ve done is to use a keyword in the start of each message, and then seperate the values with the “|” character. So a message may look like this:
“DATA|490|40517.9328222222|1|6|11345|11347|11344|11345|106|40517.8494212963”
I set the read buffer size to 1024, as most of the messages will be within that length. However sometimes I may send rapidly many short messages where several together are less than 1024 characters, and it seems then it will be read in one go. And if I send a message longer than 1024 characters, it will be split. So I’m looking for some advice on how to handle this. Should I use some special characters to start and/or end each message? Would appreciate some tips on how you do this..
The simplest way would be to send the message length at the beginning of each message, serialized in such a way that it will work on little-endian and big-endian hardware.
This could help your receiver preallocate its receive buffer efficiently too.