Re: [問題] 如何用C語言,取得本機的gateway和DNS資訊…

看板Linux作者時間17年前 (2008/07/20 02:01), 編輯推噓0(000)
留言0則, 0人參與, 最新討論串3/3 (看更多)
取得 ip 部份,程式碼如下: #include <unistd.h> #include <stdio.h> #include <arpa/inet.h> #include <net/if.h> #include <sys/ioctl.h> int main (int argc, char **argv) { int i, socket_h; char *ip; struct ifreq ifr; struct sockaddr_in *sin = (struct sockaddr_in *) &ifr.ifr_addr; sin = (struct sockaddr_in *) &ifr.ifr_addr; socket_h = socket (PF_INET, SOCK_STREAM, 0); for (i=1;;i++) { ifr.ifr_ifindex = i; if (ioctl (socket_h, SIOCGIFNAME, &ifr) < 0) break; if (ioctl (socket_h, SIOCGIFADDR, &ifr) < 0) continue; ip = inet_ntoa (sin->sin_addr); printf ("%s: %s\n", ifr.ifr_name,ip); } close (socket_h); return 0; } -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 現代人普遍的現象: 「小學而大遺」、「捨本而逐末」 「以偏而概全」、「因噎而廢食」 -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- ※ Origin: SayYA 資訊站 <bbs.sayya.org> ◆ From: kendlee.sayya.org
文章代碼(AID): #18WYla00 (Linux)
文章代碼(AID): #18WYla00 (Linux)