Read only desired amount of bytes using Boost.Asio

巧了我就是萌 提交于 2021-01-28 14:26:51

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!