获取机器的IP地址 [英] Get the IP address of the machine

查看:190
本文介绍了获取机器的IP地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此问题几乎与先前询问的获取IP地址相同本地计算机 - 问题。但是,我需要找到 Linux Machine 的IP地址。



所以:我如何 - - 检测我的应用程序运行的linux服务器的IP地址。服务器将至少有两个IP地址,我需要一个特定的网络(公共网络中的一个)。



我确定有





为了使事情更清楚, b


  • 服务器显然会有localhost:127.0.0.1

  • 服务器将有一个内部:172.16.xx

  • 服务器将有一个外部(公共)IP地址:80.190.xx



我需要找到外部IP地址来绑定我的应用程序。显然,我也可以绑定到INADDR_ANY(这实际上是我现在做的)。

解决方案

我发现ioctl解决方案在os x上有问题(这是POSIX兼容所以应该类似​​于linux)。但是getifaddress()会让你很容易做同样的事情,它对我在os x 10.5和下面应该是一样的。



我做了一个快速下面的例子将打印所有的机器的IPv4地址,(你也应该检查getifaddrs是否成功,即返回0)。



我更新了显示IPv6地址。

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

int main(int argc,const char * argv []){
struct ifaddrs * ifAddrStruct = NULL;
struct ifaddrs * ifa = NULL;
void * tmpAddrPtr = NULL;

getifaddrs(& ifAddrStruct);

for(ifa = ifAddrStruct; ifa!= NULL; ifa = ifa-> ifa_next){
if(!ifa-> ifa_addr){
continue;
}
if(ifa-> ifa_addr-> sa_family == AF_INET){//检查是IP4
//是有效的IP4地址
tmpAddrPtr =& ((struct sockaddr_in *)ifa-> ifa_addr) - > sin_addr;
char addressBuffer [INET_ADDRSTRLEN];
inet_ntop(AF_INET,tmpAddrPtr,addressBuffer,INET_ADDRSTRLEN);
printf(%s IP地址%s\\\
,ifa-> ifa_name,addressBuffer);
} else if(ifa-> ifa_addr-> sa_family == AF_INET6){//检查它是IP6
//是一个有效的IP6地址
tmpAddrPtr =& sockaddr_in6 *)ifa-> ifa_addr) - > sin6_addr;
char addressBuffer [INET6_ADDRSTRLEN];
inet_ntop(AF_INET6,tmpAddrPtr,addressBuffer,INET6_ADDRSTRLEN);
printf(%s IP地址%s\\\
,ifa-> ifa_name,addressBuffer);
}
}
if(ifAddrStruct!= NULL)freeifaddrs(ifAddrStruct);
return 0;
}


This Question is almost the same as the previously asked Get the IP Address of local computer-Question. However I need to find the IP address(es) of a Linux Machine.

So: How do I - programmatically in C++ - detect the IP addresses of the linux server my application is running on. The servers will have at least two IP addresses and I need a specific one (the one in a given network (the public one)).

I'm sure there is a simple function to do that - but where?


To make things a bit clearer:

  • The server will obviously have the "localhost": 127.0.0.1
  • The server will have an internal (management) IP address: 172.16.x.x
  • The server will have an external (public) IP address: 80.190.x.x

I need to find the external IP address to bind my application to it. Obviously I can also bind to INADDR_ANY (and actually that's what I do at the moment). I would prefer to detect the public address, though.

解决方案

I found the ioctl solution problematic on os x (which is POSIX compliant so should be similiar to linux). However getifaddress() will let you do the same thing easily, it works fine for me on os x 10.5 and should be the same below.

I've done a quick example below which will print all of the machine's IPv4 address, (you should also check the getifaddrs was successful ie returns 0).

I've updated it show IPv6 addresses too.

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

int main (int argc, const char * argv[]) {
    struct ifaddrs * ifAddrStruct=NULL;
    struct ifaddrs * ifa=NULL;
    void * tmpAddrPtr=NULL;

    getifaddrs(&ifAddrStruct);

    for (ifa = ifAddrStruct; ifa != NULL; ifa = ifa->ifa_next) {
        if (!ifa->ifa_addr) {
            continue;
        }
        if (ifa->ifa_addr->sa_family == AF_INET) { // check it is IP4
            // is a valid IP4 Address
            tmpAddrPtr=&((struct sockaddr_in *)ifa->ifa_addr)->sin_addr;
            char addressBuffer[INET_ADDRSTRLEN];
            inet_ntop(AF_INET, tmpAddrPtr, addressBuffer, INET_ADDRSTRLEN);
            printf("%s IP Address %s\n", ifa->ifa_name, addressBuffer); 
        } else if (ifa->ifa_addr->sa_family == AF_INET6) { // check it is IP6
            // is a valid IP6 Address
            tmpAddrPtr=&((struct sockaddr_in6 *)ifa->ifa_addr)->sin6_addr;
            char addressBuffer[INET6_ADDRSTRLEN];
            inet_ntop(AF_INET6, tmpAddrPtr, addressBuffer, INET6_ADDRSTRLEN);
            printf("%s IP Address %s\n", ifa->ifa_name, addressBuffer); 
        } 
    }
    if (ifAddrStruct!=NULL) freeifaddrs(ifAddrStruct);
    return 0;
}

这篇关于获取机器的IP地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆