- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试将 c++11 lambda 与 boost::asio::async_read_until 一起使用,如下所示:
void TCPSession::readData()
{
auto self(shared_from_this());
boost::asio::async_read_until(socket_, buffer_, "\r\n",
[this, self](const boost::system::error_code& ec, std::size_t length)
{
log()->debug("received %lu bytes && ec: %d", length, ec); //use boost::log for compile time checking
std::ostringstream ss;
ss << &buffer_;
log()->debug("Received data: %s",ss.str());
if (!ec){
log()->debug("We never get here!");
[... process data ...]
readData();
}
}
);
}
我观察到的是长度=0。 ec 被适本地设置为 boost EOF 代码。
当我将成员(即 buffer_)转换为字符串时,我也会看到数据。
任何有关初始化长度的建议都会有所帮助。
示例输出:
2014-10-09 14:47:56.968142 [tcp-session] debug: received 0 bytes && ec: asio.misc:2
2014-10-09 14:47:56.974593 [tcp-session] debug: Received data: test7.2.3.5 0.18722307655 2014-10-09 14:47:55.343591
test7.2.3.5 -0.0691607464759 2014-10-09 14:47:55.343837
test7.2.3.5 0.151471040421 2014-10-09 14:47:55.344017
test7.2.3.5 0.172789025585 2014-10-09 14:47:55.344211
test7.2.3.5 0.0409326066787 2014-10-09 14:47:55.344406
最佳答案
尝试
printf("received %lu bytes", length);
// ...
printf("Received data: %s", ss.str().c_str());
或者更好的是,
std::cout << "received " << length << " bytes\n";
// ...
std::cout << "Received data: " << ss.str();
%d
不是 size_t 的正确格式。 (这就是发明 C++ 的原因。几十年前)
注意:如果您启用警告 (-Wall -Wextra -pedantic
),您的编译器会告诉您大部分内容
现在, docs say 那个
// The number of bytes in the streambuf's get
// area up to and including the delimiter.
// 0 if an error occurred.
因此,由于 ec 是 EOF,这说明 length
也是 0。
下面的示例打印: Live On Coliru
received 0 bytes, ec: End of file
Received data: hello world
byebye
(假设响应服务器发送“hello world\n”);
#include <boost/asio.hpp>
#include <boost/enable_shared_from_this.hpp>
#include <boost/make_shared.hpp>
#include <cstring>
#include <iostream>
struct TCPSession : boost::enable_shared_from_this<TCPSession>
{
void readData();
TCPSession(boost::asio::io_service& svc) : socket_(svc) {
using boost::asio::ip::tcp;
socket_.connect(tcp::endpoint { {}, 6767 });
}
boost::asio::ip::tcp::socket socket_;
boost::asio::streambuf buffer_;
};
void TCPSession::readData()
{
auto self(shared_from_this());
boost::asio::async_read_until(socket_, buffer_, "\r\n",
[this, self](const boost::system::error_code& ec, std::size_t length)
{
std::cout << "received " << length << " bytes, ec: " << ec.message() << "\n";
std::ostringstream ss;
ss << &buffer_;
std::cout << "Received data: " << ss.str();
if (!ec){
std::cout << "We never get here!";
//[... process data ...]
readData();
}
std::cout << "byebye\n";
}
);
}
int main()
{
boost::asio::io_service svc;
auto sess = boost::make_shared<TCPSession>(svc);
sess->readData();
svc.run();
}
关于c++ - async_read_until 的 Lambda 不初始化长度参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26288318/
我正在尝试使用 boost asio 库来实现网络编程。这是当一些数据(以“##”结尾)到达端点时调用的代码。 { boost::asio::async_read_until(m_socket
我在此处记录的函数中实现第三个参数时遇到问题: http://www.boost.org/doc/libs/1_39_0/doc/html/boost_asio/reference/async_rea
我在使用 async_read_until 时遇到问题。我最初使用的是 async_receive,但我的服务器的响应增长到不止一个数据包,我遇到了一个问题,即 async_receive 在收到第一
我正在尝试使用 boost::shared_ptr与 async_read_until并且无法让它工作。这是我的简化代码: 标题 boost::asio::io_service io_service;
我正在使用带有 '\n' 分隔符的 asio::async_read_until 来支持从服务器获取字符数据的 TCP 客户端。此服务器不断发送 '\n' 终止行;准确地说,它可以同时写入单行或多行的
在 boost::asio::async_read_until 之后,我面临周期性的无限等待处理程序调用。这出现在两种情况下: 在服务器上,尽管客户端 boost::asio::async_write
我正在尝试将 c++11 lambda 与 boost::asio::async_read_until 一起使用,如下所示: void TCPSession::readData() { auto
我正在尝试使用自定义匹配函数作为 boost::asio 组合操作的一部分。当传入 std::move(*this) 作为异步操作的处理程序时,VS2017 无法使用自定义匹配条件推断参数。 组合操作
我想知道如何限制使用 async_read_until 读取的字节大小。从现在开始,我将它与定界符字符一起使用,但我想更改。这是我所做的: void Client::doRead()
我正在学习如何将 Boost:asio 库与串行端口一起使用。我使用同步写入和读取编写了一些代码,现在我想使用异步,但它不起作用。简单示例: void readHandler(const boost:
我正在尝试修改来自 boost asio 的 echo 服务器示例,当我尝试使用 boost::asio::async_read_until 时遇到了问题。这是代码: #include #i
我遇到了一个不寻常的问题。我有一个 C++ Boost.ASIO 网络服务器,为了处理传入的请求,我正在使用以下代码: boost::asio::async_read_until( socke
我有一个关于使用 boost asio 的 async_read_until 的一般性问题。文档说调用处理程序时缓冲区内可能有更多数据。有没有什么办法可以解决这个问题,并在序列条件匹配后立即阻止缓冲区
我正在尝试在本地网络上的两台计算机之间建立连接,一台使用 Boost Asio C++ TCP 异步服务器示例的略微修改版本,另一台使用 NodeJS。 tcp_client.js : var net
我正在从串行设备读取,其中必须特别请求每条消息。例如。您发送请求并获得带有序列化负载的响应。 每条消息按顺序包含这些部分: 序言(2 字节,“$M”) HEADER(3字节,包含有效载荷长度N) 有效
有没有办法结合两个条件读取,直到找到匹配字符或收到 128 个字节? 可以限制 streambuf 的大小: inBuf = std::make_shared(limit_size); 但是当处理程序
我正在使用以下方法: boost::asio::streambuf io_bufstream; boost::asio::async_read_until(socket_, io_bufstream,
我正在用 boost asio 制作一个 Irc 机器人,我在编译它时遇到了一些问题,而且错误不是真正可读的。 似乎其中一个async_read_until进行得不是很好,但我不知道为什么。 这是 g
我使用了来自 boost 的异步 tcp 服务器示例,它与我的应用程序正在做的很接近。下面的代码示例是一个完整的示例。 首先,我开始异步读取操作,直到分隔符 char。在这种情况下,它是 http h
我正在修改 Boost Asio 回显示例以使用 async_read_until 逐字读取输入。即使我使用 async_read_until,所有发送的数据似乎都是从套接字读取的。有人可以建议: #
我是一名优秀的程序员,十分优秀!