问题
I'm just creating a very simple C++ class that provides me a few methods, like connect()
and read()
, instead of exposing all the Boost.Asio socket calls.
Right now, I'm trying to find out how to create a method that reads only the desired amount of bytes:
SocketClient::read(int bytes, char* data); //reads desired amount of bytes and puts them in data, size of data>bytes!
Unfortunately, I found no read_byte
function in Boost.Asio. I do not want to drop bytes that have been received, but not yet read.
Here is my class.
回答1:
The read function seems to provide exactly what you need:
This function is used to read a certain number of bytes of data from a stream. The call will block until one of the following conditions is true:
- The supplied buffers are full. That is, the bytes transferred is equal to the sum of the buffer sizes. - An error occurred.
Example usage:
boost::asio::read(stream, boost::asio::buffer(data, size));
来源:https://stackoverflow.com/questions/15416270/read-only-desired-amount-of-bytes-using-boost-asio