使用resolver::async_resolve时出现系统错误955

发布于 2024-12-08 07:02:16 字数 2603 浏览 1 评论 0原文

有时,当我从 tcp::resolver 使用 async_resolve 方法时,会收到系统错误 995。 下面的代码显示了相关的代码行。

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

#include <iostream>

class connection
{
public:
    connection(boost::asio::io_service& io_service)
        : m_resolver(io_service), m_socket(io_service)
        {
        }

    void async_connect(
            std::string const& host,
            std::string const& service,
            boost::asio::ip::tcp::socket::protocol_type tcp = boost::asio::ip::tcp::v4()
            )
        {
            m_resolver.async_resolve(
                    boost::asio::ip::tcp::resolver::query(tcp, host, service),
                    boost::bind(
                        &connection::resolve_handler, 
                        this, 
                        boost::asio::placeholders::error,
                        boost::asio::placeholders::iterator
                        )
                    );
        }

protected:
    virtual void on_connected(boost::system::error_code const& connect_error)
    {
        if (!connect_error)
        {
            std::cout << "Connection established. Endpoint: " << m_socket.remote_endpoint()
                      << std::endl;
        }
        else
            std::cout << "error: " << connect_error << std::endl;
    }

private:
    void connect_handler(boost::system::error_code const& connect_error)
    {
        on_connected(connect_error);
    }

    void resolve_handler(
            boost::system::error_code const& resolve_error,
            boost::asio::ip::tcp::resolver::iterator endpoint_iterator
            )
        {
            if (!resolve_error)
            {
                m_socket.async_connect(
                        *endpoint_iterator,
                        boost::bind(
                            &connection::connect_handler, 
                            this,
                            boost::asio::placeholders::error
                            )
                        );
            }
        }

private:
    boost::asio::ip::tcp::socket m_socket;
    boost::asio::ip::tcp::resolver m_resolver;
};

int main()
{
    boost::asio::io_service io_service;

    connection foo(io_service);
    foo.async_connect("www.google.de", "http");

    io_service.run();
    return 0;
}

995 (0x3E3) - ERROR_OPERATION_ABORTED 表示:

由于线程退出或 I/O 操作已中止 申请请求。

但我不知道为什么。我认为有些事情超出了范围,但我不知道到底是什么。希望你能帮助我。提前致谢!

At times I get the System Error 995 when using the async_resolve method from an tcp::resolver.
The code below shows the relevant code lines.

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

#include <iostream>

class connection
{
public:
    connection(boost::asio::io_service& io_service)
        : m_resolver(io_service), m_socket(io_service)
        {
        }

    void async_connect(
            std::string const& host,
            std::string const& service,
            boost::asio::ip::tcp::socket::protocol_type tcp = boost::asio::ip::tcp::v4()
            )
        {
            m_resolver.async_resolve(
                    boost::asio::ip::tcp::resolver::query(tcp, host, service),
                    boost::bind(
                        &connection::resolve_handler, 
                        this, 
                        boost::asio::placeholders::error,
                        boost::asio::placeholders::iterator
                        )
                    );
        }

protected:
    virtual void on_connected(boost::system::error_code const& connect_error)
    {
        if (!connect_error)
        {
            std::cout << "Connection established. Endpoint: " << m_socket.remote_endpoint()
                      << std::endl;
        }
        else
            std::cout << "error: " << connect_error << std::endl;
    }

private:
    void connect_handler(boost::system::error_code const& connect_error)
    {
        on_connected(connect_error);
    }

    void resolve_handler(
            boost::system::error_code const& resolve_error,
            boost::asio::ip::tcp::resolver::iterator endpoint_iterator
            )
        {
            if (!resolve_error)
            {
                m_socket.async_connect(
                        *endpoint_iterator,
                        boost::bind(
                            &connection::connect_handler, 
                            this,
                            boost::asio::placeholders::error
                            )
                        );
            }
        }

private:
    boost::asio::ip::tcp::socket m_socket;
    boost::asio::ip::tcp::resolver m_resolver;
};

int main()
{
    boost::asio::io_service io_service;

    connection foo(io_service);
    foo.async_connect("www.google.de", "http");

    io_service.run();
    return 0;
}

995 (0x3E3) - ERROR_OPERATION_ABORTED means:

The I/O operation has been aborted because of either a thread exit or
an application request.

But I have no idea why. I think something goes out of scope but I don't know what exactly. Hope you can help me. Thanks in advance!

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

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

发布评论

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

评论(1

双马尾 2024-12-15 07:02:16

有时我们对最明显的事情视而不见。 main 函数应如下所示:

int main()
{
    boost::asio::io_service io_service;

    connection conn(io_service);                    // Perhaps we should actually declare the object if
    conn.async_connect("www.google.de", "http");    // we want to use it beyond this line ...

    io_service.run();
    return 0;
}

Sometimes we're blind to the most obvious. This is how the main-function should look like:

int main()
{
    boost::asio::io_service io_service;

    connection conn(io_service);                    // Perhaps we should actually declare the object if
    conn.async_connect("www.google.de", "http");    // we want to use it beyond this line ...

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