Linux C语言 获取本机(所有网卡)IP地址(IPV4)
Posted ygmdream
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Linux C语言 获取本机(所有网卡)IP地址(IPV4)相关的知识,希望对你有一定的参考价值。
1、根据ioctl机制打印当前所有网卡
代码:
#include <sys/ioctl.h>
#include <net/if.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include <stdio.h>
int get_local_ip(char *ip)
int fd, intrface, retn = 0;
struct ifreq buf[INET_ADDRSTRLEN];
struct ifconf ifc;
if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) >= 0)
ifc.ifc_len = sizeof(buf);
// caddr_t,linux内核源码里定义的:typedef void *caddr_t;
ifc.ifc_buf = (caddr_t)buf;
if (!ioctl(fd, SIOCGIFCONF, (char *)&ifc))
intrface = ifc.ifc_len/sizeof(struct ifreq);
while (intrface-- > 0)
if (!(ioctl(fd, SIOCGIFADDR, (char *)&buf[intrface])))
ip=(inet_ntoa(((struct sockaddr_in*)(&buf[intrface].ifr_addr))->sin_addr));
printf("IP:%s\\n", ip);
close(fd);
return 0;
int main()
char ip[64];
memset(ip, 0, sizeof(ip));
get_local_ip(ip);
return 0;
编译:
[root@localhost get_ip]# gcc -Wall -g ip.c
[root@localhost get_ip]# ./a.out
IP:192.168.2.53
IP:192.168.2.55
IP:127.0.0.1
代码解析:
<netinet/in.h>中定义了 #define INET_ADDRSTRLEN 16
ifreq、ifconf 、ioctl参考:http://blog.csdn.net/wl_haanel/article/details/4536236
2、通过枚举网卡打印当前所有网卡
代码:
#include <stdio.h>
#include <ifaddrs.h>
#include <netinet/in.h>
#include <string.h>
#include <arpa/inet.h>
int get_local_ip(char *ip)
struct ifaddrs *ifAddrStruct;
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;
inet_ntop(AF_INET, tmpAddrPtr, ip, INET_ADDRSTRLEN);
printf("%s IP Address:%s\\n", ifAddrStruct->ifa_name, ip);
ifAddrStruct=ifAddrStruct->ifa_next;
//free ifaddrs
freeifaddrs(ifAddrStruct);
return 0;
int main()
char ip[16];
memset(ip, 0, sizeof(ip));
get_local_ip(ip);
return 0;
编译:
[root@localhost get_ip]# gcc -Wall -g ip.c
[root@localhost get_ip]# ./a.out
lo IP Address:127.0.0.1
eth0 IP Address:192.168.2.55
eth1 IP Address:192.168.2.53
代码解析:
API接口可查看man 7 netdevice
3、多网卡情况下,将所有IP地址以字符串形式打印出,用逗号(“,”)隔开,形式如:“127.0.0.1,192.168.2.55,192.168.2.53”
代码:
#include <stdio.h>
#include <ifaddrs.h>
#include <netinet/in.h>
#include <string.h>
#include <arpa/inet.h>
int get_local_ip(char *ips)
struct ifaddrs *ifAddrStruct;
void *tmpAddrPtr=NULL;
char ip[INET_ADDRSTRLEN];
int n = 0;
getifaddrs(&ifAddrStruct);
while (ifAddrStruct != NULL)
if (ifAddrStruct->ifa_addr->sa_family==AF_INET)
tmpAddrPtr=&((struct sockaddr_in *)ifAddrStruct->ifa_addr)->sin_addr;
inet_ntop(AF_INET, tmpAddrPtr, ip, INET_ADDRSTRLEN);
printf("%s IP Address:%s\\n", ifAddrStruct->ifa_name, ip);
if (n == 0)
strncat(ips, ip, INET_ADDRSTRLEN);
else
strncat(ips, ",", 1);
strncat(ips, ip, INET_ADDRSTRLEN);
n++;
ifAddrStruct=ifAddrStruct->ifa_next;
//free ifaddrs
freeifaddrs(ifAddrStruct);
return 0;
int main()
char ip[64];
memset(ip, 0, sizeof(ip));
get_local_ip(ip);
printf("IP:%s\\n", ip);
return 0;
编译:
[root@localhost get_ip]# gcc -Wall -g ip.c
[root@localhost get_ip]# ./a.out
lo IP Address:127.0.0.1
eth0 IP Address:192.168.2.55
eth1 IP Address:192.168.2.53
IP:127.0.0.1,192.168.2.55,192.168.2.53
4、多网卡情况下,将多个ip地址放入字符串数组中(去掉"127.0.0.1"地址)
代码:
#include <stdio.h>
#include <ifaddrs.h>
#include <netinet/in.h>
#include <string.h>
#include <arpa/inet.h>
int get_local_ip(char *ip_list)
struct ifaddrs *ifAddrStruct;
void *tmpAddrPtr;
char ip[INET_ADDRSTRLEN];
int n = 0;
getifaddrs(&ifAddrStruct);
while (ifAddrStruct != NULL)
if (ifAddrStruct->ifa_addr->sa_family==AF_INET)
tmpAddrPtr=&((struct sockaddr_in *)ifAddrStruct->ifa_addr)->sin_addr;
inet_ntop(AF_INET, tmpAddrPtr, ip, INET_ADDRSTRLEN);
if (strcmp(ip, "127.0.0.1") != 0)
// printf("%s IP Address:%s\\n", ifAddrStruct->ifa_name, ip);
if (n == 0)
memcpy(ip_list, ip, INET_ADDRSTRLEN);
else
memcpy(ip_list+INET_ADDRSTRLEN, ip, INET_ADDRSTRLEN);
n++;
ifAddrStruct=ifAddrStruct->ifa_next;
//free ifaddrs
freeifaddrs(ifAddrStruct);
return n;
int main()
char ip[3][INET_ADDRSTRLEN];
memset(ip, 0, sizeof(ip));
int n;
for (n=get_local_ip(*ip); n>0; n--)
printf("IP:%s\\n", ip[n-1]);
return 0;
编译:
[root@localhost get_ip]# gcc -Wall -g ip.c
[root@localhost get_ip]# ./a.out
IP:192.168.2.53
IP:192.168.2.55
CSDN 社区图书馆,开张营业! 深读计划,写书评领图书福利~以上是关于Linux C语言 获取本机(所有网卡)IP地址(IPV4)的主要内容,如果未能解决你的问题,请参考以下文章