简体   繁体   English

使用Boost.Asio读取所需的字节数

[英]Read only desired amount of bytes using 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. 我只是在创建一个非常简单的C ++类,为我提供了一些方法,例如connect()read() ,而不是公开所有Boost.Asio套接字调用。

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. 不幸的是,我没有在Boost.Asio中找到read_byte函数。 I do not want to drop bytes that have been received, but not yet read. 我不想删除已收到但尚未读取的字节。

Here is my class. 这是我的课。

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));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM