【问题标题】:Boost Asio tcp::resolver: service not foundBoost Asio tcp::resolver:找不到服务
【发布时间】:2011-07-11 09:01:36
【问题描述】:

我正在尝试使用 Boost Asio 创建一个 HTTP 客户端。我从 asio 复制了同步客户端示例,编译,然后运行。不幸的是,在我的日志中,它显示未找到服务。当我跟踪代码时,发现它是从以下代码中抛出的:

boost::asio::io_service io_service;
// Get a list of endpoints corresponding to the server name.
tcp::resolver resolver(io_service);
//->if i removed the http, it has no error
tcp::resolver::query query("host.com", "http");
//->This part throws the service not found
tcp::resolver::iterator endpoint_iterator = resolver.resolve(query);
tcp::resolver::iterator end;

谁能解释为什么找不到服务或我该如何解决这个问题?

【问题讨论】:

    标签: c++ boost service boost-asio resolver


    【解决方案1】:

    这意味着操作系统不知道哪个端口号对应于名为“http”的 TCP 服务。

    在类似 unix 的操作系统上,这意味着 http 80/tcp 行从 /etc/services 中丢失,我可以通过注释掉该行来重现 Linux 上的错误。

    如果操作系统无法配置为使用服务,您可以在解析器中使用任何服务"",并在为连接调用创建端点对象时明确指定端口号:

    tcp::endpoint connectionEndpoint(endpoint_iterator->address(), 80);
    boost::system::error_code ec;
    socket.connect(connectionEndpoint, ec);
    

    【讨论】:

    • 感谢您的回复。我回去工作时会试试这个。谢谢。
    • 您也可以将端口号作为字符串给出,例如查询(“host.com”,“80”)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-19
    • 2014-12-22
    相关资源
    最近更新 更多