boost asio:“未找到主机(授权)"; [英] boost asio: "host not found (authorative)"

查看:214
本文介绍了boost asio:“未找到主机(授权)";的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am making a program for school in which two programs communicate with each other. So far I have not been able to connect the two programs. Whenever I try to connect to localhost:8888 or 127.0.0.1:8888, the error "Host not found (authoritative)" occurs.

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

So far my code is this:

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;
    }
} 

如果有帮助,我可以使用软呢帽.

In case this helps, I use fedora.

完整的代码可以在这里找到: http://www.boost.org/doc/libs/1_36_0/doc/html/boost_asio/tutorial/tutdaytime1.html 我试图将其设置为OOP(不确定我是否做得很好)

the full code can be found here: http://www.boost.org/doc/libs/1_36_0/doc/html/boost_asio/tutorial/tutdaytime1.html i tried to make it OOP (not sure if i did a good job)

推荐答案

您应使用:

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

问题是查询的构造函数具有 默认设置的address_configured标志不会返回地址 如果回送设备是唯一具有地址的设备.只是 设置标志为0或address_configured以外的任何其他值 问题已解决.

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.

Boost Asio的主机名解析在Linux上如何工作?可以使用NSS吗?

如果您粘贴整个代码,我将为您提供更多帮助.首先,这里有一段非常有用的代码:

I will more help you if you paste the whole code. For the very beginning there is really useful piece of code here:

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

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

it works and you can test it with your local telnet.

这篇关于boost asio:“未找到主机(授权)";的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆