C++获取网卡Ip地址

C++获取网卡Ip地址,第1张

#include 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;
int compare(const void* a, const void* b)
{
    return strcmp(*(char**)a, *(char**)b);
}
int getSubnetMask(char ip[], int len)
{
    struct sockaddr_in* sin = NULL;
    struct ifaddrs* ifa = NULL, * ifList;
    string name[10];
    if (getifaddrs(&ifList) < 0)
    {
        return -1;
    }
    int i = 0;
    //找到所有网卡,并打印网卡相关信息
    for (ifa = ifList; ifa != NULL; ifa = ifa->ifa_next)
    {
        if (ifa->ifa_addr->sa_family == AF_INET)
        {
            string ifname(ifa->ifa_name);
            name[i] = ifname;
            i++;
            printf("interfaceName: %s\n", ifa->ifa_name);
            sin = (struct sockaddr_in*)ifa->ifa_addr;
            printf("ipAddress: %s\n", inet_ntoa(sin->sin_addr));
            sin = (struct sockaddr_in*)ifa->ifa_dstaddr;
            printf("broadcast: %s\n", inet_ntoa(sin->sin_addr));
            sin = (struct sockaddr_in*)ifa->ifa_netmask;
            printf("subnetMask: %s\n\n", inet_ntoa(sin->sin_addr));
        }
    }
    //打印所有非空的网卡名
    for (int i = 0; i < sizeof(name) / sizeof(name[0]); i++)
    {
        if (strcmp(name[i].c_str(), ""))
        {
            printf("name%d=%s,", i, name[i].c_str());
        }
    }
    printf("\n");
    //从小到大排序
    qsort(name, 10, sizeof(name[0]), compare);
    //查找最小网卡对应的ip
    for (int i = 0; i < sizeof(name) / sizeof(name[0]); i++)
    {
        if (strcmp(name[i].c_str(), ""))
        {
            for (ifa = ifList; ifa != NULL; ifa = ifa->ifa_next)
            {
                if (ifa->ifa_addr->sa_family == AF_INET)
                {
                    string ifname(ifa->ifa_name);
                    if (strcmp(name[i].c_str(), ifname.c_str()) == 0)
                    {
                        printf("check name=%s\n", ifname.c_str());
                        sin = (struct sockaddr_in*)ifa->ifa_addr;
                        char* ipAddress = inet_ntoa(sin->sin_addr);
                        //过滤掉local
                        if (strcmp(ipAddress, "127.0.0.1"))
                        {
                            strncpy(ip, ipAddress, len);
                            printf("last use name=%s,ip=%s", name[i].c_str(),
                                freeifaddrs(ifList);
                            return 0;
                        }
                        else
                        {
                            printf("is local, not use");
                        }
                    }
                }
            }
        }
    }
    freeifaddrs(ifList);
    return -1;
}
int main(int argc, char* argv[])
{
    char ip[64];
    if (int ret = getSubnetMask(ip, sizeof(ip)) < 0)
    {
        printf("find ip error\n");
        return -1;
    }
    printf("ip=%s\n", ip);
    return 0;
}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/3002851.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-09-27
下一篇 2022-09-27

发表评论

登录后才能评论

评论列表(0条)

保存