c++ - Asio::async_connect 和 io_service.run 不编译

标签 c++ sockets boost boost-asio

我正在学习使用 Boost ASIO。这是我的代码改编自 Boost 文档的 chat example .

class AsioCommunicationService {
AsioCommunicationService::AsioCommunicationService(
        boost::asio::io_service& io_service,
        tcp::resolver::iterator endpoint_iterator)
    : io_service_(io_service),
      socket_(io_service)
{
    boost::asio::async_connect(socket_, endpoint_iterator,
        boost::bind(&AsioCommunicationService::handle_connect, this,
        boost::asio::placeholders::error));
}



void AsioCommunicationService::handle_connect(const boost::system::error_code& error)
{
    if (!error)
    {
      boost::asio::async_read(socket_,
          boost::asio::buffer(read_msg_.data(), LampMessage::header_length),
          boost::bind(&AsioCommunicationService::handle_read_header, this,
          boost::asio::placeholders::error));
    }
}
}


class Connection
{
    m_session = std::shared_ptr<AsioCommunicationService>(
            new AsioCommunicationService(io_service, iterator));
    boost::thread t(boost::bind(&boost::asio::io_service::run, &io_service));
}

编译时,我收到一条错误消息,指出“async_connect”不是“boost::asio”的成员。显然,就 IDE 告诉我的而言,async_connect 不是 boost asio 的成员,而是 socket 的成员。我正在使用 libasiodev 1.41.3。

我试图修改对此的调用

tcp::endpoint endpoint = *endpoint_iterator;
socket_.async_connect(endpoint,
    boost::bind(&AsioCommunicationService::handle_connect, this,
    boost::asio::placeholders::error, ++endpoint_iterator));
//Got this error: instantiated from ‘boost::_bi::bind_t<boost::_bi::unspecified, void (networkService::AsioCommunicationService::*)(const boost::system::error_code&), boost::_bi::list3<boost::_bi::value<networkService::AsioCommunicationService*>, boost::arg<1> (*)(), boost::_bi::value<boost::asio::ip::basic_resolver_iterator<boost::asio::ip::tcp> > > >’

更新: 更改 handle_connect 签名以包含迭代器参数后,我成功编译了该行。但是,编译器现在停留在包含到 thread.hpp

#include <boost/thread.hpp>
//ERROR: In function ‘boost::thread&& boost::move(boost::thread&&)’:
///usr/include/boost/thread/detail/thread.hpp:349:16: error: invalid initialization of reference of type ‘boost::thread&&’ from expression of type ‘boost::thread’
//In file included from /usr/include/boost/thread/detail/thread_heap_alloc.hpp:17:0,
//             from /usr/include/boost/thread/detail/thread.hpp:13,
//             from /usr/include/boost/thread/thread.hpp:22,
//             from /usr/include/boost/thread.hpp:13,
//             from /home/son/dev/logistics/src/frameworks/networkService/NetworkConnection.cpp:13:
///usr/include/boost/thread/pthread/thread_heap_alloc.hpp: In function ‘T* boost::detail::heap_new(A1&&) [with T = boost::detail::thread_data<void (*)()>, A1 = void (*&)()]’:
///usr/include/boost/thread/detail/thread.hpp:130:95:   instantiated from here
///usr/include/boost/thread/pthread/thread_heap_alloc.hpp:24:47: error: cannot bind ‘void (*)()’ lvalue to ‘void (*&&)()’
///usr/include/boost/thread/detail/thread.hpp:43:13: error:   initializing argument 1 of ‘boost::detail::thread_data<F>::thread_data(F&&) [with F = void (*)()]’

出于某种原因,我无法通过调用 boost::thread t(boost::bind(&boost::asio::io_service::run, &io_service)) 来启动新线程; ,即使我通过对 io_service.run() 的简单调用替换它也能正常工作;

最佳答案

因为你的cb有签名

void AsioCommunicationService::handle_connect(const boost::system::error_code& error)

这个调用是不正确的。

tcp::endpoint endpoint = *endpoint_iterator;
socket_.async_connect(endpoint,
    boost::bind(&AsioCommunicationService::handle_connect, this,
    boost::asio::placeholders::error, ++endpoint_iterator));

你的最后一个参数是错误的,所以,正确的绑定(bind)是

boost::bind(&AsioCommunicationService::handle_connect, this,
        boost::asio::placeholders::error));

关于c++ - Asio::async_connect 和 io_service.run 不编译,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11718597/

相关文章:

c++ - boost .asio : can I do async_read and async_write simultaneously from one thread?

c++ - 配置 : error: no boost. 找到文件系统库

C++ 包找不到从包中的其他头文件调用的头位置

java - 我应该为 Socket 上的每个读/写创建一个新线程吗?

C++ 在指针范围结束后访问堆上的对象

c - 在 C 中通过套接字传递结构

sockets - Boost Asio,io_service每个内核仅处理一个套接字

c++ - 嵌入式 Python : Getting func obj from imported module

c++ 写运算符>,我似乎不正确地接近比较,我该如何正确地做到这一点?

c++ - 将十进制数保持在一个范围内