使用 boost::asio 将resolve_handler绑定到解析器async_resolve

发布于 2024-11-07 15:31:02 字数 868 浏览 0 评论 0 原文

我有这个代码。如何将我的方法resolve_handler与预期的迭代器和错误参数绑定?这是打破连接逻辑的正确方法吗?

void FileClient::start() 
{ 
    try {
        boost::asio::ip::tcp::resolver::query query("ip", "port");
        resolver_.async_resolve(query, boost::bind(
            &FileClient::resolve_handler, this
        ));
    }
    catch (std::exception& e)
    {
        std::cerr << "Exception: " << e.what() << "\n";
    }
}

void FileClient::resolve_handler(const boost::system::error_code &_error,
                                 boost::asio::ip::tcp::resolver::iterator _it)
{ 
    if (!_error) 
        socket_.async_connect(*_it, boost::bind(
            &FileClient::connect_handler, this, boost::asio::placeholders::error
        ));
    else 
        std::cerr << "resolve_handler error: " << _error << std::endl; 
}

I have this code. How can I bind my method resolve_handler with the expected iterator and error parameters? Is it the correct way to break down the connection logic?

void FileClient::start() 
{ 
    try {
        boost::asio::ip::tcp::resolver::query query("ip", "port");
        resolver_.async_resolve(query, boost::bind(
            &FileClient::resolve_handler, this
        ));
    }
    catch (std::exception& e)
    {
        std::cerr << "Exception: " << e.what() << "\n";
    }
}

void FileClient::resolve_handler(const boost::system::error_code &_error,
                                 boost::asio::ip::tcp::resolver::iterator _it)
{ 
    if (!_error) 
        socket_.async_connect(*_it, boost::bind(
            &FileClient::connect_handler, this, boost::asio::placeholders::error
        ));
    else 
        std::cerr << "resolve_handler error: " << _error << std::endl; 
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夕嗳→ 2024-11-14 15:31:02

boost.asio教程中有示例,例如来自此 HTTP 异步客户端

tcp::resolver::query query(server, "http");
    resolver_.async_resolve(query,
        boost::bind(&client::handle_resolve, this,
          boost::asio::placeholders::error,
          boost::asio::placeholders::iterator));

...

void handle_resolve(const boost::system::error_code& err,
                    tcp::resolver::iterator endpoint_iterator)
{
    if (!err)
    {
        // Attempt a connection to the first endpoint in the list. Each endpoint
        // will be tried until we successfully establish a connection.
         tcp::endpoint endpoint = *endpoint_iterator;
         socket_.async_connect(endpoint,
                         boost::bind(&client::handle_connect, this,
                         boost::asio::placeholders::error, ++endpoint_iterator));
    }
    else
    {
        std::cout << "Error: " << err.message() << "\n";
    }
}

(它们的 handle_connect 继续根据需要递增 endpoint_iterator

There are examples in boost.asio tutorials, for example, from this HTTP async client

tcp::resolver::query query(server, "http");
    resolver_.async_resolve(query,
        boost::bind(&client::handle_resolve, this,
          boost::asio::placeholders::error,
          boost::asio::placeholders::iterator));

...

void handle_resolve(const boost::system::error_code& err,
                    tcp::resolver::iterator endpoint_iterator)
{
    if (!err)
    {
        // Attempt a connection to the first endpoint in the list. Each endpoint
        // will be tried until we successfully establish a connection.
         tcp::endpoint endpoint = *endpoint_iterator;
         socket_.async_connect(endpoint,
                         boost::bind(&client::handle_connect, this,
                         boost::asio::placeholders::error, ++endpoint_iterator));
    }
    else
    {
        std::cout << "Error: " << err.message() << "\n";
    }
}

(their handle_connect continues to increment endpoint_iterator as necessary)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文