I'm building a library that maintainces connection over network between apps in c++. During some debuging I have found that some information I get from socket seems incomplete. It is vital this information is complete because if I decode it with protocol buffers' library, I need complete message.
The phenomenon is not constant. Sometimes I get all data from the socket, sometimes not. The test procedure looks like: start server, start client multiple times. What I get is output from function receive() that depict how values of some variables change. One of them is the size of the buffer I use to store data. That size tells me how much data is in the buffer. The clients send two messages each of size 3 (bytes) - in this particular test. I expect the buffer size to be multiple of 3. However, sometimes the size is 4!. That means program read one complete message and 1/3 of the second message. I don't understand why I always get first message complete. Otherwise, protobuf would terminate the program.
I thought tcp/ip should take care I get complete messages. My problem is I don't know the size in advance. I expect to receive full message so that I can interpret it corecty.
TCP is a stream based protocol, not a message based protocol. TCP has no idea when your messages begin or end, and so there is no guarentee that you will get a full message in a single call. There is also no guarentee that you'll get only one full message. You must buffer the data you receive, and split it into messages yourself. If you don't get a full message, store what data you have received, and wait until the rest arrives.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With