views:

239

answers:

1

In winsock, both the sync recv and the async WSARecv complete as soon as there is data available in a stream socket, regardless of the size specified (which is only the upper limit). This means that in order to read a fixed number of bytes from the stream, there should be applied some custom buffering. And unless each read is buffered seperately, it results in double buffering, that is, there are actually two buffers per socket.

Is there any way to ask winsock to buffer the data on it's own behalf and only complete the operation when the specified number of bytes become available (unless an error occurs)?

EDIT: This functionality should work for async sockets in particular.

+4  A: 

For synchronous sockets you should be able to pass the MSG_WAITALL flag to recv, which will block until your buffer is full or there is an error/disconnect.

In regards to overlapped io, then not really. Your only real option is to buffer the data. You don't really need two buffers for this, though; you can use the same buffer and just pass the buffer + offset of the end of the last read until the buffer has been filled.

Gerald
It seems to be the solution for blocking sockets, but according to MSDN it isn't supported for async sockets. see the edit.
sold
I updated my answer accordingly.
Gerald