Reputation: 103
Upvotes: 0
Views: 158
Reputation: 1200
You can form the data block passed to send this way: [ 6 bytes: data size header] [ data ]
Then call recv() in the loop reading 1 byte on each iteration. Read the ‘data size header’ (first 6 bytes) and allocate the buffer of desirable size then read the rest of bytes.
Upvotes: 0
Reputation: 11567
TCP does not preserve message boundaries. All it exposes is a stream of bytes. Therefore, the send()s that you call on one side might not correspond to the recv()s on the other side. For example, the following might happen:
Or, conversely:
Regarding your "padding" question, not that you have to check the return value of recv() to see how many bytes where actually returned in your buffer.
Upvotes: 3
Reputation: 409196
You can get any amount of data from zero up to the size you request. Always check the return value to see how much data you actually received.
To answer your questions: You can get less than a complete "response", or you can get more than a complete "response". If you get more than one full response message, you might get two, or one and a half, or anything else more than one.
Upvotes: 1