Revathy
Revathy

Reputation: 103

padding of responses in recv() api

  1. In client side,it always get full response or there is any chance for getting half of response in one sort and remaining half in another sort(for example : depending on buffer that we defined for recv() API).
  2. Is there any chances for receiving two responses padded in recv() API.

Upvotes: 0

Views: 158

Answers (3)

ChatCloud
ChatCloud

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

user1202136
user1202136

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:

  • A calls send() with 100 bytes, then calls again send() with 100 bytes
  • B calls recv() and gets 200 bytes (see Nagle's algorithm)

Or, conversely:

  • A calls send() with 1 MiB
  • B calls recv(), gets 512 KiB, then calls recv() again, gets 512 KiB (buffer space was insufficient either on the receiver, or the sender side).

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

Some programmer dude
Some programmer dude

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

Related Questions