如果您制作了网络状态指示器,这就是您可能想要做的事情。循环解析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)
最佳答案
由于您正在运行 Fedora,NetworkManager 可能正在运行并在以太网出现故障时自动从 /etc/resolv.conf
中删除 DHCP 学习的名称服务器。 (或以其他方式修改 resolv.conf
以响应接口(interface)更改)
关于c++ - 我如何调整这个 C++ 测试用例,以便它继续与新的网络设备一起工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4956915/