2

当您有移动连接时,您通常有 2 个 IP 地址,一个外部 IP,可以在http://www.whatsmyip.org/中查询

IP 结果:190.106.XX.XX

当我将我的 PC 连接到 BAM 时,我得到另一个 IP,如图所示

在此处输入图像描述

IP 结果:10.69.XX.XX

我需要获取的 ipAddress 是来自 Window 的用于使用它的地址(10.69.XX.XX)

我检查了一个 android 应用程序并将这个结果显示为 RMNET0

并在java中告诉我他使用了request.getRemoteHost()。用于获取此数据

我将不胜感激的任何信息

提前致谢

4

1 回答 1

1

我已经成功尝试过,希望对您有所帮助

+(NSString *)getIpLocally:(NSString *)networkInterface ipVersion:(int)ipVersion
{
    if(ipVersion != 4 && ipVersion != 6)
    {
        NSLog(@"getIpLocally unknown version of IP: %i",ipVersion);
        return nil;
    }

    NSString *networkInterfaceRef;

    if ([networkInterface isEqualToString:@"ios_cellular"])
    {
        networkInterfaceRef = @"pdp_ip0";
    }
    else if([networkInterface isEqualToString:@"ios_wifi"])
    {
        networkInterfaceRef = @"en0"; //en1 on simulator if mac on wifi
    }
    else
    {
        NSLog(@"getIpLocally unknown interface: %@",networkInterface);
        return nil;
    }

    NSString *address = nil;
    struct ifaddrs *addrs = NULL;
    struct ifaddrs *cursor = NULL;
    struct sockaddr_in *s4;
    struct sockaddr_in6 *s6;
    char buf[64];
    int success = 0;

    // retrieve the current interfaces - returns 0 on success
    success = getifaddrs(&addrs);
    if (success == 0)
    {
        // Loop through linked list of interfaces
        cursor = addrs;
        while(cursor != NULL)
        {
            if((ipVersion == 4 && cursor->ifa_addr->sa_family == AF_INET) ||
               (ipVersion == 6 &&  cursor->ifa_addr->sa_family == AF_INET6))
            {
                NSLog(@"Network Interface: %@",[NSString stringWithUTF8String:cursor->ifa_name]);

                // Check if interface is en0 which is the wifi connection on the iPhone
                if([[NSString stringWithUTF8String:cursor->ifa_name] isEqualToString:networkInterfaceRef])
                {
                    if(ipVersion == 4)
                    {
                        s4 = (struct sockaddr_in *)cursor->ifa_addr;

                        if (inet_ntop(cursor->ifa_addr->sa_family, (void *)&(s4->sin_addr), buf, sizeof(buf)) == NULL)
                        {
                            NSLog(@"%s: inet_ntop failed for v4!\n",cursor->ifa_name);
                        }
                        else{
                            address = [NSString stringWithUTF8String:buf];
                        }
                    }
                    if (ipVersion == 6)
                    {
                        s6 = (struct sockaddr_in6 *)(cursor->ifa_addr);

                        if (inet_ntop(cursor->ifa_addr->sa_family, (void *)&(s6->sin6_addr), buf, sizeof(buf)) == NULL)
                        {
                            NSLog(@"%s: inet_ntop failed for v6!\n",cursor->ifa_name);
                        }
                        else{
                            address = [NSString stringWithUTF8String:buf];
                        }
                    }
                }
            }
            cursor = cursor->ifa_next;
        }
    }
    // Free memory
    freeifaddrs(addrs);
    return address;
}

要显示 IP 地址,您可以使用:

NSLog(@"ip v4: %@",[NSString getIpLocally:@"ios_cellular" ipVersion:4]);
于 2013-05-03T00:31:34.647 回答