ASIO直接从异步解析获取tcp端点

时间:2017-10-31 17:37:59

标签: c++ asynchronous tcp boost-asio

我希望使用ASIO独立库(而不是Boost ASIO),我正在尝试设置客户端以连接到特定端口上的服务器。

我在porthopper示例中看到,可以在不必处理迭代器的情况下获取端点。

asio::io_service io_service;

// Determine the location of the server.
tcp::resolver resolver(io_service);
tcp::resolver::query query(host_name, port);
tcp::endpoint remote_endpoint = *resolver.resolve(query);

我正在尝试使用解析器async_resolve()成员函数来解析查询。

这是我目前的代码:

asio::io_service IOService;
asio::ip::tcp::resolver resolver(IOService);
asio::ip::tcp::resolver::query query(ADDRESS, PORT);
resolver.async_resolve(query,
            [this](const tcp::endpoint srvEndpoint, std::error_code error)
            {
                IOService->post(
                    [this, error, srvEndpoint]
                    {
                         handle_resolve_handler(error, srvEndpoint);
                    });
            });

有没有办法执行porthopper示例中显示的内容但是异步执行它?

1 个答案:

答案 0 :(得分:1)

哦,但是

tcp::endpoint remote_endpoint = *resolver.resolve(query);

非常多地处理迭代器!它用它来解除引用。注意可爱的明星?这是pointer indirection运营商。

至于你的电话:

resolver.async_resolve(query,
            [this](const tcp::endpoint srvEndpoint, std::error_code error)
            {
                IOService->post(
                    [this, error, srvEndpoint]
                    {
                         handle_resolve_handler(error, srvEndpoint);
                    });
            });

这不满足完成处理程序要求。实际上,尝试使用BoostAsio²编译它会产生一系列错误: Live On Coliru

main.cpp:12:14:   required from here
/usr/local/include/boost/asio/ip/basic_resolver.hpp:163:5: error: static assertion failed: ResolveHandler type requirements not met
     BOOST_ASIO_RESOLVE_HANDLER_CHECK(
     ^
/usr/local/include/boost/asio/ip/basic_resolver.hpp:163:5: error: no match for call to '(Demo::doResolve()::<lambda(boost::asio::ip::tcp::endpoint, boost::system::error_code)>) (const boost::system::error_code&, const boost::asio::ip::basic_resolver_iterator<boost::asio::ip::tcp>&)'
     BOOST_ASIO_RESOLVE_HANDLER_CHECK(

The docs say

enter image description here

瞧,再看看你的迭代器了!这绝非偶然。该库的设计使得异步调用将始终返回相同的数据,而不管所选择的接口.¹

将它拼凑在一起: Live On Coliru

#include <boost/asio.hpp>
#include <iostream>

namespace asio = boost::asio;
using boost::system::error_code;
using asio::ip::tcp;

struct Demo {
    Demo(asio::io_service& svc) : _svc(svc) {}
    void doResolve() {
        resolver.async_resolve(query, [this](error_code error, tcp::resolver::iterator it) {
                tcp::endpoint ep = error? tcp::endpoint{} : *it;
                _svc.post([this, error, ep] { handle_resolve_handler(error, ep); });
            });
    }

  private:
    asio::io_service& _svc;
    tcp::resolver resolver     {_svc};
    tcp::resolver::query query {"www.google.com", "https"};

    void handle_resolve_handler(error_code ec, tcp::endpoint srvEndpoint) {
        std::cout << "handle_resolve_handler: " << ec.message() << " " << srvEndpoint << "\n";
    }
};

int main() {
    asio::io_service svc;
    Demo x(svc);
    x.doResolve();

    svc.run();
}

Prints³:

handle_resolve_handler: Success 216.58.213.196:443

¹比照使用协同程序(yieldyield[ec]),asio::use_future等时的区别:How to set error_code to asio::yield_context

²基本上s/boost::system::error_code/std::error_code/

³在具有网络访问权限的系统上