【问题标题】:boost:asio:read_until issue with boost::bindboost:asio:read_until 问题与 boost::bind
【发布时间】:2012-07-16 13:44:18
【问题描述】:

我在编译这段代码时遇到问题

/usr/include/boost/bind/bind_mf_cc.hpp:91:5: 错误:正在初始化 'boost::_bi::bind_t, typename boost::_bi::list_av_4::type> boost:: 的参数 5 bind(R (T::*)(B1, B2, B3), A1, A2, A3, A4) [其中 R = void; T = tcpReader; B1 = const boost::system::error_code&; B2 = 无符号整数; B3 = boost::asio::basic_streambuf&; A1 = tcpReader*; A2 = boost::system::error_code; A3 = 无符号整数; A4 = boost::asio::basic_streambuf;类型名 boost::_bi::list_av_4::type = boost::_bi::list4, boost::_bi::value, boost::_bi::value, boost::_bi::value >]

void tcpReader::handle_read(const boost::system::error_code& ec, std::size_t bytes_transferred, boost::asio::streambuf& buf)
// inside a class method
boost::asio::streambuf buf;
boost::asio::async_read_until(*sock,buf,"\n" ,
                              boost::bind(&tcpReader::handle_read,this,error,buf.size(),buf)
                              );

关于问题是什么的任何想法?我知道我遗漏了一些简单的东西,但我不知道是不是我必须使用 boot::buffer ?

提前致谢

【问题讨论】:

  • 你应该分别使用boost::asio::placeholders::errorboost::asio::placeholders::bytes_transferred而不是errbuf.size()吗?
  • 您可能还需要用boost::ref(buf) 包装最后一个参数buf

标签: c++ boost boost-asio boost-bind


【解决方案1】:

在我看来,async_read_until() 的所有重载都需要 2 个参数。您传递一个带有 3 个参数的函数。可能您想将流作为额外参数传递,将其绑定到函数以获取具有 2 个参数的函数。

boost::bind( &tcpReader::handle_read, this, _1, _2, boost::ref( buf ) )

会将您的成员函数“转换”为需要 2 个参数的东西。 boost::ref() 将您的缓冲区包装为参考,否则将进行复制。

亲切的问候 托斯滕

【讨论】:

  • 现在看来,由于某种原因,我的函数从未被调用过,我没有看到 handle_read 任何想法的任何输出?
  • @gda2004 从你的例子来看,流缓冲区似乎没有与任何类型的 tcp 套接字相关联。
  • @gda2004 你是对的,所以 *sock 是表示套接字的表达式。如果没有触发您的回调,我会假设没有写入任何数据,或者没有读取“\n”。您可以使用 Wire Shark 之类的工具来分析网络流量。
猜你喜欢
  • 1970-01-01
  • 2023-04-07
  • 2012-05-26
  • 2012-04-18
  • 1970-01-01
  • 1970-01-01
  • 2022-07-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多