Linux下获取本机IP

通过查找网卡信息来获取所有你想要的IP 具体可以通过 ifr->ifr_flags 来做判断操作

#include <stdio.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <sys/ioctl.h>
#include <net/if.h>

int main(void)
{
    int s;
    struct ifconf conf;
    struct ifreq *ifr;
    char buff[BUFSIZ];
    int num;
    int i;

    s = socket(PF_INET, SOCK_DGRAM, 0);
    conf.ifc_len = BUFSIZ;
    conf.ifc_buf = buff;

    ioctl(s, SIOCGIFCONF, &conf);
    num = conf.ifc_len / sizeof(struct ifreq);
    ifr = conf.ifc_req;

    for(i=0;i < num;i++)
    {
        struct sockaddr_in *sin = (struct sockaddr_in *)(&ifr->ifr_addr);

        ioctl(s, SIOCGIFFLAGS, ifr); 
        if(
                ((ifr->ifr_flags & IFF_LOOPBACK) == 0) 
                && (ifr->ifr_flags & IFF_UP))
        {
            printf("%s (%s)\n",
                    ifr->ifr_name,
                    inet_ntoa(sin->sin_addr));
        }
        ifr++;
    }

    return 0;
}


你可能感兴趣的:(Linux下获取本机IP)