获取方法:
调用getifaddrs接口来获取,调用freeifaddrs接口来释放内存
需包含:
#include <sys/types.h> #include <ifaddrs.h>
参考代码:
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <ifaddrs.h>
int main(int argc, char* argv[])
{
struct ifaddrs *ifaddr_lst;
struct ifaddrs *ifaddr_node;
int family;
char host[NI_MAXHOST];
if(getifaddrs(&ifaddr_lst) < 0)
{
perror("getifaddrs");
return -1;
}
for(ifaddr_node = ifaddr_lst; ifaddr_node != NULL; ifaddr_node = ifaddr_node->ifa_next)
{
/* ifa_addr可能为空,*/
if(NULL == ifaddr_node->ifa_addr)
{
continue;