[英]Async_read_until limit size of bytes read (Boost::asio)
i want to know how can i limit the size of bytes to read with async_red_until . 我想知道如何限制使用async_red_until读取的字节大小。 From now i used it with a delimter character but i want to change.
从现在起,我将其与斜线字符一起使用,但我想进行更改。 Here is what i do :
这是我的工作:
void Client::doRead()
{
boost::asio::async_read_until(m_socket,
m_buffer,
'\n',
boost::bind(&Client::handleRead,
shared_from_this(),
boost::asio::placeholders::error));
}
You could use transfer_exactly
( https://www.boost.org/doc/libs/1_68_0/doc/html/boost_asio/reference/transfer_exactly.html ) 您可以使用
transfer_exactly
( https://www.boost.org/doc/libs/1_68_0/doc/html/boost_asio/reference/transfer_exactly.html )
Note that there is no guarantee you'll always read the amount, eg if the sending side closes the connection early. 请注意,这不能保证您将始终读取该金额,例如,如果发送方提前关闭了连接。 So check
bytes_transferred
as well as the error_code
. 因此,检查
bytes_transferred
以及error_code
。
In many cases you can simply use async_read
instead, with aa fixed buffer: 在许多情况下,您可以简单地使用
async_read
,并使用async_read
固定缓冲区:
std::vector<char> m_buffer;
// ...
m_buffer.resize(24); // receive no more than 24 characters
boost::asio::async_read(m_socket,
boost::asio::buffer(m_buffer),
boost::bind(&Client::handleRead, shared_from_this(),
boost::asio::placeholders::error));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.