我该如何适应这个 C++测试用例以便它继续与新的网络设备一起工作?

发布于 2024-10-16 22:44:33 字数 2278 浏览 2 评论 0原文

如果您制作了网络状态指示器,这就是您可能想要做的事情。循环解析DNS并返回ping结果。

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

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

从下面的日志中可以看到,当切换时,它从“非权威”变为“权威”,但从未找到主机。

(请告诉我如何在预代码块内进行 markdown 转义 << 还是我必须使用 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)

This is the sort of thing you might want to do if you made a network status indicator. In a loop, resolve the DNS and return the result of ping.

So, how can we make sure DNS resolve continues to work when the network device is changed? In this case by disconnecting the builtin network followed by connecting a GPRS modem.

I'm using Fedora 13 but I'm guessing it's the same behavior on many other nix-based systems.

As you can see from the log below, when switching it goes from "non-authoritative" to "authoritative" but the host is never found.

(Please tell me how to markdown escape << inside a pre code block or do i have to use HTML code?)


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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦一生花开无言 2024-10-23 22:44:34

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

Since you're running Fedora, NetworkManager is probably running and automatically removing the DHCP-learned nameservers from /etc/resolv.conf when the Ethernet goes down. (Or otherwise modifying resolv.conf in response to the interface changes)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文