boost::asio::async_read_until 不会读取所有消息
boost::asio::async_read_until does not read all message
boost::asio::async_read_until 不会读取所有数据,而是在消息中间挂起。
boost::asio::async_read_until(socket, response, "n", boost::asio::placeholders::error));
[&](const boost::system::error_code &err2, std::size_t lenght)
{
RUN_CHECK;
if (!err2)
{
RUN_CHECK;
std::string r = this->response_get();
/*
.. do stuff here
*/
}
else
{
report_error(err2);
}
}
知道出了什么问题吗?在到达""之前不应该让async_read_until读取?
std::string r = this->response_get();
无论您在response_get()
中做什么,您都没有正确使用length
。这意味着任何超出分隔符("n"
(的字符也可能被消耗,这意味着下一次读取将缺少开始。
另一个可能起作用的问题是当您的缓冲区不是动态的(例如 asio::streambuf
(,并且您没有预留足够的容量。然后,异步读取将在缓冲区填充后完成,即使未收到分隔符也是如此。
这些都不能解释"挂起"。如果观察到挂起,请检查服务器(发送端(和/或网络基础结构(使用网络监视器工具检查邮件是否实际到达(。
下面是要自包含的代码的快速修复:
#include <boost/asio.hpp>
#include <iostream>
#define RUN_CHECK
do {
std::cout << "Here:" << __LINE__ << "n";
} while (0)
struct X {
boost::asio::io_service io;
boost::asio::ip::tcp::socket socket{io};
X() {
socket.connect({ {}, 6767 });
}
boost::asio::streambuf response;
void foo() {
boost::asio::async_read_until(socket, response, "n",
[&](const boost::system::error_code &ec, std::size_t length) {
RUN_CHECK;
if (!ec) {
RUN_CHECK;
std::string r = this->response_get(length);
/*
.. do stuff here
*/
} else {
report_error(ec);
}
});
}
std::string response_get(std::size_t length) {
std::string result;
std::copy_n(boost::asio::buffers_begin(response.data()), length, back_inserter(result));
return result;
}
void report_error(boost::system::error_code ec) {
std::cout << "Error: " << ec.message() << "n";
}
};
int main() {
X x;
x.foo();
x.io.run();
}
相关文章:
- 应该如何读取堆缓冲区溢出错误消息?
- C++输出与Python不同的消息(从串行端口读取)
- 读取系统() 命令错误响应消息
- 有没有办法在不使用 set_buff() 的情况下从文件中读取并填充消息增益
- boost::asio::async_read_until 不会读取所有消息
- 套接字记录未读取任何消息
- 为什么 protobuf 只读取最后一条消息作为输入结果
- 协议缓冲区-读取所有消息中通用的标头(嵌套消息)
- 使用Boost Json读取JSON消息
- 如何正确读取套接字消息(C++)
- 无法读取从客户端发送的所有消息
- boost asio读取动态大小消息
- 读取功能失败时的成功消息
- 在 c++ 中使用消息结构读取/填充数据缓冲区的正确方法是什么?
- 正在读取RSysLog tcp消息
- boost-asio-async_read:读取消息添加到自身
- Boost::asio TCP服务器——从客户端读取消息
- 套接字:客户端读取消息时多线程不起作用
- 如何从文件中读取消息,仅修改单词
- 使用协议缓冲区从文件读取消息时出现问题