C++获取本机IP地址列表
2018-07-20 来源:open-open
/*
* main.c
* ----------------------------------------------
* 2013-01-09 chrisniu1984@gmail.com
*
* [BUILD]
* gcc -o main main.c -Wall
*
*/
#include <stdio.h>
#include <netdb.h>
#include <ifaddrs.h>
// 系统定义的结构,贴在这里方便大家查阅,也可man getifaddrs查看。
// struct ifaddrs {
// struct ifaddrs *ifa_next; /* Next item in list */
// char *ifa_name; /* Name of interface */
// unsigned int ifa_flags; /* Flags from SIOCGIFFLAGS */
// struct sockaddr *ifa_addr; /* Address of interface */
// struct sockaddr *ifa_netmask; /* Netmask of interface */
// union {
// struct sockaddr *ifu_broadaddr;
// /* Broadcast address of interface */
// struct sockaddr *ifu_dstaddr;
// /* Point-to-point destination address */
// } ifa_ifu;
// #define ifa_broadaddr ifa_ifu.ifu_broadaddr
// #define ifa_dstaddr ifa_ifu.ifu_dstaddr
// void *ifa_data; /* Address-specific data */
// };
int main(int argc, char **argv)
{
struct ifaddrs *ifa;
if (getifaddrs(&ifa) != 0) {
return -1;
}
for (; ifa!=NULL; ifa=ifa->ifa_next) {
struct sockaddr_in *sin = (struct sockaddr_in*)ifa->ifa_addr;
if (sin->sin_family != AF_INET) {
continue;
}
unsigned char *b = (unsigned char *)(&sin->sin_addr.s_addr);
printf("%s\t%u.%u.%u.%u\n", ifa->ifa_name, b[0], b[1], b[2], b[3]);
}
return 0;
}
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点!
本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。
上一篇:C++编写的页面淘汰算法FIFO
下一篇:C++编写的页面淘汰算法OPT
最新资讯
热门推荐