简体   繁体   English

获取LAN IP并打印

[英]get LAN ip and print it

Does anyone know how do I get my LAN IP and print it on the screen. 有谁知道我如何获取我的LAN IP并将其打印在屏幕上。 *I don't mean on shell, but in c programming. *我不是在shell上,而是在c编程中。 **I will appreciate if you'll post me a sample code. **如果您能向我发布示例代码,我将不胜感激。

There's a few approaches; 有几种方法。 first, you could set up a connection to a known peer using connect(2) and then read the local socket 'name' with getsockname(2) . 首先,您可以使用connect(2)与已知对等方的connect(2) ,然后使用getsockname(2)读取本地套接字'name'。 This is a pretty poor mechanism, but it is easy. 这是一个很差的机制,但是很容易。

But, getsockname(2) will only report one IP address, when a machine may have thousands of IP addresses, and which IP is returned will depend in part upon the peer that you chose! 但是,当一台机器可能具有数千个IP地址并且返回哪个IP时, getsockname(2)仅报告一个 IP地址,这在一定程度上取决于您选择的对等方! So, not very reliable. 因此,不是很可靠。

A much better answer is to use rtnetlink(7) to read the IP addresses for the machine directly from the kernel: you would send RTM_GETADDR messages to the kernel for each interface on the machine and read the answers back. 更好的答案是使用rtnetlink(7)直接从内核读取计算机的IP地址:您将向计算机上每个接口的内核发送RTM_GETADDR消息,然后将答案读回。 Your best bet for understanding how to use this is probably to read the source code for the ip program. 了解如何使用此功能的最佳选择可能是阅读ip程序的源代码。

Another option is to use the SIOCGIFCONF ioctl(2) on an IP socket. 另一个选择是在IP套接字上使用SIOCGIFCONF ioctl(2) This interface isn't as flexible as the rtnetlink(7) interface, so it might not always be correct, but it'll be a mid-way point. 该接口不如rtnetlink(7)界面灵活,因此它可能并不总是正确的,但这将是一个中间点。 The ifconfig(8) utility uses this approach for displaying ifconfig -a output. ifconfig(8)实用程序使用此方法显示ifconfig -a输出。 Again, your best bet would be to read the source. 同样,您最好的选择是阅读源代码。 (There is some slight documentation in ioctl_list(2) .) ioctl_list(2)有一些ioctl_list(2)文档。)

The getifaddrs() function from <ifaddrs.h> is the simplest way to get the current interfaces and corresponding addresses: <ifaddrs.h>getifaddrs()函数是获取当前接口和相应地址的最简单方法:

#include <stdio.h>
#include <ifaddrs.h>
#include <netinet/in.h>
#include <arpa/inet.h>

int main()
{
    struct ifaddrs *iflist, *iface;

    if (getifaddrs(&iflist) < 0) {
        perror("getifaddrs");
        return 1;
    }

    for (iface = iflist; iface; iface = iface->ifa_next) {
        int af = iface->ifa_addr->sa_family;
        const void *addr;
        char addrp[INET6_ADDRSTRLEN];

        switch (af) {
            case AF_INET:
                addr = &((struct sockaddr_in *)iface->ifa_addr)->sin_addr;
                break;
            case AF_INET6:
                addr = &((struct sockaddr_in6 *)iface->ifa_addr)->sin6_addr;
                break;
            default:
                addr = NULL;
        }

        if (addr) {
            if (inet_ntop(af, addr, addrp, sizeof addrp) == NULL) {
                perror("inet_ntop");
                continue;
            }

            printf("Interface %s has address %s\n", iface->ifa_name, addrp);
        }
    }

    freeifaddrs(iflist);
    return 0;
}

gethostbyname should help you to do this. gethostbyname应该可以帮助您做到这一点。 Example: 例:

GetLocalAddress()
{
  char ac[80];

  // Get my host name
  if (gethostname(ac, sizeof(ac)) != -1)
  {
    printf("Host name: %s\n", ac);

    // Find IP addresses
    struct hostent* p_he = gethostbyname(ac);

    if (p_he != 0)
    {
        for (int i=0; p_he->h_addr_list[i] != 0; ++i)
        {
            struct in_addr addr;
            memcpy(&addr, p_he->h_addr_list[i], sizeof(struct in_addr));
            printf("IP address %d: %s\n", i, inet_ntoa(addr));
        }
    }
}

You might want to filter to remove 127.0.0.1 from the list. 您可能需要过滤以从列表中删除127.0.0.1。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM