有時候程式需要獲取網絡卡的ip位址我們習慣於ifconfig獲取,有的人也使用popen用ifconfig來獲取但是現在我將介紹下通過系統介面獲取,**如下:
#include #include #include #include int getsubnetmask(char *ip)
for (ifa = iflist; ifa != null; ifa = ifa->ifa_next)}}
freeifaddrs(iflist);
return 0;
}int main()
; getsubnetmask(ip);
printf("%s\n", ip);
return 0;
}
linux獲取本機網絡卡ip位址
include include include include include include include include include define eth name eth0 int main strncpy ifr.ifr name,eth name,ifnamsiz ifr.ifr n...
Linux系統獲取裝置網絡卡ip位址的幾種方法
這種方法通過呼叫linux提供的應用介面函式 getifaddrs getnameinfo,遍歷裝置所有網絡卡獲取相關配置資訊 getnameinfo函式獲取本地網絡卡ip位址時,有乙個bug 獲取到的ip位址可能被內環位址覆蓋,而獲取不到真正的ip位址。例子 include include inc...
linux C語言獲取網絡卡對應IP位址
int get gw ip char eth,char ipaddr get ip address strncpy ifr.ifr name,eth,if namesize ifr.ifr name ifnamsiz 1 0 if ioctl sock fd,siocgifaddr,ifr 0 me...