使用 getaddrinfo() C 函数获取本地 IP 地址?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2146191/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 22:16:16  来源:igfitidea点击:

Obtaining local IP address using getaddrinfo() C function?

c++cgetaddrinfo

提问by Goles

I'm trying to obtain my local (not the external) IP address using the getaddrinfo() function, but I saw the examples provided here, and they where too complex for my needs. Also saw other posts and most of them really wanted to get the external IP, not the local one.

我正在尝试使用 getaddrinfo() 函数获取我的本地(而不是外部)IP 地址,但我看到了此处提供的示例,它们对于我的需求来说太复杂了。也看了其他帖子,大部分真的想拿到外网IP,不是本地IP。

Could anyone provide a link to a simple example (or a simple example) about how to obtain my own local IP address using this function ?

任何人都可以提供有关如何使用此功能获取我自己的本地 IP 地址的简单示例(或简单示例)的链接?

Just to be clear when I say local, if a router is 192.168.0.1, my local IP address could be something like 192.168.0.x( just an example ).

只是在我说本地时要清楚,如果路由器是192.168.0.1,我的本地 IP 地址可能是这样的192.168.0.x(只是一个例子)。

回答by caf

getaddrinfo()isn't for obtaining your local IP address - it's for looking up names and/or services to socket addresses. To obtain the local IP address(es), the function you want is getifaddrs()- here's a minimal example:

getaddrinfo()不是用于获取您的本地 IP 地址 - 它用于查找套接字地址的名称和/或服务。要获取本地 IP 地址,您需要的功能是getifaddrs()- 这是一个最小的示例:

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <net/if.h>
#include <ifaddrs.h>
#include <errno.h>

int main(int argc, char *argv[])
{
    struct ifaddrs *myaddrs, *ifa;
    void *in_addr;
    char buf[64];

    if(getifaddrs(&myaddrs) != 0)
    {
        perror("getifaddrs");
        exit(1);
    }

    for (ifa = myaddrs; ifa != NULL; ifa = ifa->ifa_next)
    {
        if (ifa->ifa_addr == NULL)
            continue;
        if (!(ifa->ifa_flags & IFF_UP))
            continue;

        switch (ifa->ifa_addr->sa_family)
        {
            case AF_INET:
            {
                struct sockaddr_in *s4 = (struct sockaddr_in *)ifa->ifa_addr;
                in_addr = &s4->sin_addr;
                break;
            }

            case AF_INET6:
            {
                struct sockaddr_in6 *s6 = (struct sockaddr_in6 *)ifa->ifa_addr;
                in_addr = &s6->sin6_addr;
                break;
            }

            default:
                continue;
        }

        if (!inet_ntop(ifa->ifa_addr->sa_family, in_addr, buf, sizeof(buf)))
        {
            printf("%s: inet_ntop failed!\n", ifa->ifa_name);
        }
        else
        {
            printf("%s: %s\n", ifa->ifa_name, buf);
        }
    }

    freeifaddrs(myaddrs);
    return 0;
}

回答by robert

Pass the hostname after using gethostname(), to gethostbyname()

使用后将主机名传递gethostname()gethostbyname()

int gethostname(char *hostname, size_t size);