首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何调整此C++测试用例,使其继续与新的网络设备一起工作?

如何调整此C++测试用例,使其继续与新的网络设备一起工作?
EN

Stack Overflow用户
提问于 2011-02-10 20:12:41
回答 1查看 1.4K关注 0票数 3

如果您创建了一个网络状态指示器,这可能是您想要做的事情。在循环中,解析DNS并返回ping结果。

那么,我们如何确保DNS解析在网络设备更改时继续工作?在这种情况下,通过断开内置网络,然后连接GPRS调制解调器。

我使用的是Fedora 13,但我猜它在许多其他基于nix的系统上也是一样的。

正如你可以从下面的日志中看到的,当它从“非授权”切换到“授权”时,主机永远也找不到。

(请告诉我如何在预代码块中标记转义<<,还是必须使用HTML代码?)

代码语言:javascript
复制
#include <iostream>
#include <boost/asio.hpp>
int main(int argc, char *argv[]) {
    std::string DNS = "www.google.com";
    while (1) {
        try {
            boost::asio::io_service io_service;
            boost::asio::ip::tcp::resolver resolver(io_service);
            boost::asio::ip::tcp::resolver::query query(DNS.c_str(), "");
            boost::asio::ip::tcp::resolver::iterator iter = resolver.resolve(query);
            boost::asio::ip::tcp::resolver::iterator end;
            if (iter != end) {
                boost::asio::ip::address addr = (iter++)->endpoint().address();
                std::cout << addr.to_string() << std::endl;
            }
        }
        catch (std::exception &e) {
            std::cerr << "Error: GetIP():" << e.what() << std::endl;
        }
        usleep(1000000);
    }
}
代码语言:javascript
复制
[test@Test-Live-1010001 Downloads]$ g++ -o test -lboost_system -lpthread testcase_hostname_not_found.cpp
[test@Test-Live-1010001 Downloads]$ ./test
209.85.149.106
209.85.149.99
209.85.149.104
209.85.149.147
209.85.149.106
209.85.149.103
209.85.149.105
209.85.149.99
209.85.149.103
209.85.149.103
209.85.149.106
Error: GetIP(): Host not found (non-authoritative), try again later
Error: GetIP(): Host not found (non-authoritative), try again later
Error: GetIP(): Host not found (non-authoritative), try again later
Error: GetIP(): Host not found (non-authoritative), try again later
Error: GetIP(): Host not found (non-authoritative), try again later
Error: GetIP(): Host not found (non-authoritative), try again later
Error: GetIP(): Host not found (non-authoritative), try again later
Error: GetIP(): Host not found (non-authoritative), try again later
Error: GetIP(): Host not found (non-authoritative), try again later
Error: GetIP(): Host not found (authoritative)
Error: GetIP(): Host not found (authoritative)
Error: GetIP(): Host not found (authoritative)
EN

回答 1

Stack Overflow用户

发布于 2011-02-10 21:32:37

由于您运行的是Fedora,因此当以太网发生故障时,NetworkManager可能正在运行并自动从/etc/resolv.conf中删除DHCP学习的名称服务器。(或以其他方式修改resolv.conf以响应接口更改)

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4956915

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档