0
votes

I am working on TCP client server application using c++.third party lib are now allowed in this project.

Here exchange between client server takes using well define protocol format.once the client receives the packet it will send it for parsing.I have protocol manager which will take care of the parsing activity.

I have following doubt
When the data arrives at client from the network, the OS buffers it until application calls recv() function.

So two message msg1 and msg2 arrives at the buffer a call to recv will return msg1+msg2. Now this may result in failure of the parsing activity.

My queries
1. whether above mentioned assumption is correct or not ?
2. If above mentioned assuption is correct then how can resolve this issue.

2
Take a look at this question, It talks about how you should handle messages from server over TCP.cpx
Thanks cpx I think it should workChris_vr
You are correct: recv might return both messages in their entirety. In other situations, recv might return only part of a message, or it might return all of msg1 and part of msg2. In short, recv has no knowledge of, and no respect for, the sequence of send calls that generated the data flow. What TCP will guarantee is this: all of the bytes it does return will be in order, with nothing skipped. (N.b. other rules apply for UDP or other protocols.)Robᵩ

2 Answers

0
votes

TCP emulates a stream, so in TCP there is no notion of messages. If you want messages, your application has to have some protocol to seperate them.

UDP does have a messages, so there it is possible to retrieve separate messages.

0
votes

You can use a LV protocol (Length-Value) for your message. Encode the message length in the first (1-4) bytes, then put the message. Something like this for example : 14"Hello world\0"

In your server, when a client is sending something you'll have to recv() the first (1-4) bytes then recv() the length of the message.