如何调整此C ++测试用例,以便继续使用新的网络设备?

时间:2011-02-10 12:12:41

标签: c++ linux networking boost posix

如果您制作了网络状态指示器,则可能需要执行此操作。在循环中,解析DNS并返回ping的结果。

那么,当网络设备发生变化时,我们如何确保DNS解析继续有效?在这种情况下,通过断开内置网络,然后连接GPRS调制解调器。

我正在使用Fedora 13,但我猜它在许多其他基于nix的系统上都是相同的行为。

正如您在下面的日志中所看到的,当切换时,它从“非权威”变为“权威”,但永远不会找到主机。

(请告诉我如何降价转义<<在预编码块内或我是否必须使用HTML代码?)


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

[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)

1 个答案:

答案 0 :(得分:0)

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