【问题标题】:Getting "resolve: Host not found (authoritative)" exception when using boost asio使用 boost asio 时出现“解决:找不到主机(权威)”异常
【发布时间】:2020-09-11 14:52:40
【问题描述】:

我正在尝试创建一个简单的代理服务器。我想在port_ 连接到远程host_(可能是google.com),并转发请求req_buf_to_send。以下是我的代码:

boost::asio::io_service io_service;
tcp::resolver resolver(io_service);
tcp::resolver::query query(host_, port_string, boost::asio::ip::resolver_query_base::numeric_service);
tcp::resolver::iterator endpoint_iterator = resolver.resolve(query);
tcp::socket socket(io_service);
tcp::endpoint connectionEndpoint(endpoint_iterator->endpoint().address(), port_);
boost::system::error_code ec;
socket.connect(connectionEndpoint, ec);
boost::asio::write(socket, req_buf_to_send);

当我运行上述代码并向其发送需要转发的请求时,代码失败并引发异常:

Exception: resolve: Host not found (authoritative)

我也对上述代码进行了单元测试,但也失败了,说:

C++ exception with description "resolve: Service not found" thrown in the test body.

所以,我的解析器逻辑似乎有问题。谁能帮帮我?

谢谢。

【问题讨论】:

    标签: c++ sockets boost tcp boost-asio


    【解决方案1】:

    主机或服务可能未知或为空。以下是解析器如何根据输入失败的一些演示:

    #include <boost/asio.hpp>
    #include <iostream>
    #include <iomanip>
    #include <boost/range/istream_range.hpp>
    
    int main() {
        boost::asio::io_context io;
        boost::asio::ip::tcp::resolver r(io);
    
        for (auto q : {
                boost::asio::ip::tcp::resolver::query 
                // these are good:
                {"google.com", ""},              // port 0
                {"google.com", "ftp"},           // port 21
                {"google.com", "80"},            // port 80
                // these ar bad:
                {"google.ggg", ""},              // host not found
                {"google.com", "bogus_service"}, // service not found
                {"google.com", "-1"},            // service not found
                {"", ""},                        // host not found
            })
        {
            try
            {
                for (auto ep : boost::make_iterator_range(r.resolve(q), {}))
                    std::cout << std::quoted(q.host_name()) << " -> " << ep.endpoint() << "\n";
            } catch(std::exception const& e) {
                std::cout << std::quoted(q.host_name()) << " " << e.what() << "\n";
            }
        }
    }
    

    打印:

    "google.com" -> 172.217.168.206:0
    "google.com" -> [2a00:1450:400e:80c::200e]:0
    "google.com" -> 172.217.168.206:21
    "google.com" -> [2a00:1450:400e:80c::200e]:21
    "google.com" -> 172.217.168.206:80
    "google.com" -> [2a00:1450:400e:80c::200e]:80
    "google.ggg" resolve: Host not found (authoritative)
    "google.com" resolve: Service not found
    "google.com" resolve: Service not found
    "" resolve: Host not found (authoritative)
    

    现在,如果您的系统不知道服务名称,可能是因为您在一些精简的极简安装(docker 容器或嵌入式系统?)上运行。解决方案是安装标准服务数据库(例如,将 IANA 列表放入 /etc/services

    在 Debian 上,这将是 sudo apt-get install netbase 的问题

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-04-29
      • 2021-09-26
      • 1970-01-01
      • 2021-02-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多