2011-02-10 51 views
3

這是你可能想要做的事情,如果你做了網絡狀態指示器。在一個循環中,解析DNS並返回ping的結果。如何適應這個C++測試用例,以便它能夠繼續使用新的網絡設備?

那麼,如何確保在網絡設備更改時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); 
    } 
} 

[[email protected] Downloads]$ g++ -o test -lboost_system -lpthread testcase_hostname_not_found.cpp 
[[email protected] 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) 

回答

0

既然你運行Fedora,NetworkManager的可能運行並在以太網關閉時自動從/etc/resolv.conf中刪除DHCP學習的名稱服務器。 (或以其他方式修改resolv.conf以響應接口更改)

+0

我可以確認這是發生了什麼事。這不是很正常嗎,因爲大多數域名服務器不允許其他IP向他們請求。 所以問題是,因爲我想從C++做到這一點,我無法控制?有沒有人可以踢,並告訴他們重讀resolv.conf? – Kasreyn 2011-02-10 13:46:06

相關問題