如果您创建了一个网络状态指示器,这可能是您想要做的事情。在循环中,解析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)发布于 2011-02-10 21:32:37
由于您运行的是Fedora,因此当以太网发生故障时,NetworkManager可能正在运行并自动从/etc/resolv.conf中删除DHCP学习的名称服务器。(或以其他方式修改resolv.conf以响应接口更改)
https://stackoverflow.com/questions/4956915
复制相似问题