在iOS上提升ASIO错误"Host not found"

Boost ASIO error "Host not found" on iOS

本文关键字:Host not found 错误 ASIO iOS      更新时间:2023-10-16

我有一个应用程序,每秒通过本地网络发送一次多播消息。这段代码在Windows和macOS上运行良好,在iOS上运行良好,除非网络没有连接到互联网。

        boost::asio::ip::tcp::resolver resolver(m_IOservice);
        boost::system::error_code ec;
        boost::asio::ip::tcp::resolver::query query(boost::asio::ip::host_name(),"");
        boost::asio::ip::tcp::resolver::iterator it = resolver.resolve(query, ec);
        boost::asio::ip::address interface_ip_address;
        while(it!=boost::asio::ip::tcp::resolver::iterator())
        {
            interface_ip_address = (it++)->endpoint().address();
            if(interface_ip_address.is_v4())
            {
                // Message sending code here
            }
        }

问题发生在resolver.resolve(),我得到一个异常"主机未找到(权威)"作为错误。

我不确定我真的理解为什么这个错误会发生,当然也不知道如何修复它。有人能给我解释一下吗?

host_name返回什么?它可能返回一个不能解析的名称。

如果您只想要localhost,请使用boost::asio::ip::addressv4()甚至boost::asio::ip::address::from_string("127.0.0.1")

或者在您的/etc/hosts文件中添加一个条目:

 127.0.0.1 thehostnametoresolvetolocalhost