原文地址:http://blog.csdn.net/langeldep/article/details/8306603
版权声明:本文为博主原创文章,未经博主允许不得转载。
[cpp] view
plain copy
-
#include
-
#include
-
#include
-
#include
-
#include
-
#include
-
#include
-
#include
-
#include
-
#include
-
#include
-
#include
-
#include
-
#include
-
#include
-
#include
-
#include
-
#include
-
#include
-
#include
-
-
-
int main (int argc, const char * argv[])
-
{
-
-
struct ifaddrs * ifAddrStruct=NULL;
-
void * tmpAddrPtr=NULL;
-
-
getifaddrs(&ifAddrStruct);
-
-
while (ifAddrStruct!=NULL)
-
{
-
if (ifAddrStruct->ifa_addr->sa_family==AF_INET)
-
{
-
-
tmpAddrPtr = &((struct sockaddr_in *)ifAddrStruct->ifa_addr)->sin_addr;
-
char addressBuffer[INET_ADDRSTRLEN];
-
inet_ntop(AF_INET, tmpAddrPtr, addressBuffer, INET_ADDRSTRLEN);
-
printf("%s IPV4 Address %s
", ifAddrStruct->ifa_name, addressBuffer);
-
}
-
else if (ifAddrStruct->ifa_addr->sa_family==AF_INET6)
-
{
-
-
tmpAddrPtr=&((struct sockaddr_in *)ifAddrStruct->ifa_addr)->sin_addr;
-
char addressBuffer[INET6_ADDRSTRLEN];
-
inet_ntop(AF_INET6, tmpAddrPtr, addressBuffer, INET6_ADDRSTRLEN);
-
printf("%s IPV6 Address %s
", ifAddrStruct->ifa_name, addressBuffer);
-
}
-
ifAddrStruct = ifAddrStruct->ifa_next;
-
}
-
return 0;
-
}
编译
gcc -g -O0 -o showip main.c
执行
./showip
显示如下
[root@localhost ~]# ./showip
lo IPV4 Address 127.0.0.1
eth0 IPV4 Address 192.168.1.103
lo IPV6 Address ::
eth0 IPV6 Address 0:0:fe80::20c:29ff