c++ - boost asio : "host not found (authorative)"

标签 c++ boost boost-asio

我正在为学校制作一个程序,其中两个程序相互通信。 到目前为止,我还无法连接这两个程序。 每当我尝试连接到 localhost:8888 或 127.0.0.1:8888 时,都会出现错误“找不到主机(权威)”。

到目前为止我的代码是这样的:

连接.cpp

Connection::Connection(std::string Arg) {
    try
    {
        tcp::resolver resolver(io_service);
        cout<<Arg<<endl;
        tcp::resolver::query query(Arg, "daytime");
        tcp::resolver::iterator endpoint_iterator = resolver.resolve(query);
        tcp::resolver::iterator end;

        tcp::socket socket(io_service);
        socket_p = &socket;
        boost::system::error_code error = boost::asio::error::host_not_found;
        while (error && endpoint_iterator != end)
        {
           socket.close();
           socket.connect(*endpoint_iterator++, error);
        }
        if (error)
           throw boost::system::system_error(error);
    }
    catch (std::exception& e)
    {
        std::cerr << e.what() << std::endl;
    }
}
void Connection::Receiver() {
    try{
        for (;;)
        {
            boost::array<char, 128> buf;
            boost::system::error_code error;

            size_t len = socket_p->read_some(boost::asio::buffer(buf), error);

            if (error == boost::asio::error::eof)
                break; // Connection closed cleanly by peer.
            else if (error)
                throw boost::system::system_error(error); // Some other error.

            std::cout.write(buf.data(), len);
        }
    }
    catch (std::exception& e)
    {
        std::cerr << e.what() << std::endl;
    }
} 

如果这有帮助,我会使用 fedora。

编辑: 完整的代码可以在这里找到: http://www.boost.org/doc/libs/1_36_0/doc/html/boost_asio/tutorial/tutdaytime1.html 我试着让它 OOP(不确定我是否做得很好)

最佳答案

你应该使用:

tcp::resolver::query query(host, PORT, boost::asio::ip::resolver_query_base::numeric_service);

The problem was that the constructor for query has the address_configured flag set by default which won't return an address if the loopback device is the only device with an address. By just settings flags to 0 or anything other than address_configured the problem is fixed.

How does Boost Asio's hostname resolution work on Linux? Is it possible to use NSS?

如果您粘贴整个代码,我会为您提供更多帮助。对于一开始,这里有一段非常有用的代码:

http://boost.2283326.n4.nabble.com/Simple-telnet-client-demonstration-with-boost-asio-asynchronous-I-O-td2583017.html

它可以工作,您可以使用本地 telnet 对其进行测试。

关于c++ - boost asio : "host not found (authorative)",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12542460/

相关文章:

c++ - boost 线程同步

c++ - 如何检测处理程序是否为 ASIO strand wrap 并通过 strand 调用它?

c++ - 为什么 CUDA 固定内存这么快?

c++ - boost asio ssl简单加密程序

c++ - 对我的类(class)实现删除功能

c++ - 迭代器和简单的赋值/析构函数

c++ - async_read_until 的 Lambda 不初始化长度参数

c++ - Boost::Asio:async_read 的问题

c++ - 从 C++ 中的抽象类派生的类中没有销毁阶段

c++ - 使用 for_each 时出错