0

有时我会在System Error 995使用. 下面的代码显示了相关的代码行。async_resolvetcp::resolver

#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 操作已中止。

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

4

1 回答 1

1

有时我们对最明显的事情视而不见。这就是main-function 的样子:

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;
}
于 2011-10-03T14:17:17.297 回答