嵌入式linux系統重新加載DNS配置

        對於通過wifi等移動網絡聯網的的嵌入式系統而言,當wifi環境發生變化(如連接新的wifi)時,DNS路由器地址很可能會被更新。在這種情況下,應用程序的DNS也要及時更新,不然應用程序的網絡可能會不通,當通過http等協議訪問服務器的時候,返回“couldn‘t resolve hostname”錯誤。

        此時,通過如下函數可以更新應用程序的DNS配置。

       #include <netinet/in.h>
       #include <arpa/nameser.h>
       #include <resolv.h>

        res_init();

        函數說明如下:

The  res_init()  function  reads the configuration files (see resolv.conf(5)) to get the default domain name, search order and name server address(es).  If no
       server is given, the local host is tried.  If no domain is given, that associated with the local host is used.  It can  be  overridden  with  the  environment
       variable LOCALDOMAIN.  res_init() is normally executed by the first call to one of the other functions.

其它,與網絡地址相關的常用的函數:

gethostbyname():獲取指定的域名或者主機名對應的IP。
gethostbyaddr():獲取指定IP地址對應的主機信息。

res_query()。


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章