我正在使用此代码进行阅读
socket_.async_read_some(boost::asio::buffer(data_, max_length),
boost::bind(&session::handle_read, this,
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred));
这是写作
boost::asio::async_write(socket_,
boost::asio::buffer(data_, bytes_transferred),
boost::bind(&session::handle_write, this,
boost::asio::placeholders::error));
其中socket_是socket,max_length是枚举,值为1024,data_是char数组,长度为max_length。
但我想用streambuf替换char数组缓冲区。我试过了
boost::asio::streambuf streamBuffer;
socket_.async_read_some(boost::asio::buffer(streamBuffer),
boost::bind(&session::handle_read, this,
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred));
但是没有用。我该怎么办?
答案 0 :(得分:16)
您需要从streambuf
获取mutable_buffers_type
作为async_read_some
的第一个参数。
boost::asio::streambuf streamBuffer;
boost::asio::streambuf::mutable_buffers_type mutableBuffer =
streamBuffer.prepare(max_length);
socket_.async_read_some(boost::asio::buffer(mutableBuffer),
boost::bind(&session::handle_read, this,
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred));