使用Boost.Asio读取所需的字节数 [英] Read only desired amount of bytes using Boost.Asio
问题描述
我只是创建一个非常简单的C ++类,该类为我提供了一些方法,例如 connect()
和 read()
,而不是公开所有的Boost.Asio套接字调用.
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!
不幸的是,我在Boost.Asio中没有找到 read_byte
函数.我不想删除已收到但尚未读取的字节.
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.
推荐答案
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.
示例用法:
boost::asio::read(stream, boost::asio::buffer(data, size));
这篇关于使用Boost.Asio读取所需的字节数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!