【问题标题】:How can I get the amount of transferred bytes on asynchronous reading boost asio c++如何在异步读取 boost asio c++ 上获取传输的字节数
【发布时间】:2013-04-22 17:00:56
【问题描述】:
正如我在 Boost::asio 中看到的,异步读取函数不会返回传输的字节数,但普通读取函数会返回。使用 async_read_some 时如何获取传输的字节数? (参数:缓冲区,处理程序)
【问题讨论】:
标签:
c++
sockets
asynchronous
boost-asio
【解决方案1】:
All forms of async_read 期待表单的“ReadHandler”回调
void handler(
const boost::system::error_code& error, // Result of operation.
std::size_t bytes_transferred // Number of bytes copied into the
// buffers. If an error occurred,
// this will be the number of
// bytes successfully transferred
// prior to the error.
);
回调的第二个参数是读取的字节数。
【解决方案2】:
读取完成后,异步读取函数调用“处理程序”函数(或函数对象)。传输的字节数被传递给该函数;函数的签名必须是:
void handler(
const boost::system::error_code& error, // Result of operation.
std::size_t bytes_transferred // Number of bytes read.
);
读取处理程序的要求记录在 here