gethostbyname()在iOS6中失败 [英] gethostbyname( ) failed in iOS6

查看:179
本文介绍了gethostbyname()在iOS6中失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用gethostbyname()来获取设备IP。在iOS5中,它运行良好。但在iOS6中,gethostbyname()返回的主机值为NULL。以下是我获取设备当前本地IP的代码。

I use gethostbyname() to get the device IP. In iOS5, it works well. But in iOS6, the host value returned by gethostbyname() is NULL. Below is my code to get the current local IP of the device.

// retun the host name
- (NSString *)hostname
{
    char baseHostName[256];
    int success = gethostname(baseHostName, 255);
    if (success != 0) return nil;
    baseHostName[255] = '\0';

#if !TARGET_IPHONE_SIMULATOR
    return [NSString stringWithFormat:@"%s.local", baseHostName];
#else
    return [NSString stringWithFormat:@"%s", baseHostName];
#endif
}

// return IP Address
- (NSString *)localIPAddress
{
    struct hostent *host = gethostbyname([[self hostname] UTF8String]);
    if (!host) {
        herror("resolv");
        return nil;
    }
    struct in_addr **list = (struct in_addr **)host->h_addr_list;
    return [NSString stringWithCString:inet_ntoa(*list[0]) encoding:NSUTF8StringEncoding];
}

请注意,模拟器适用于iOS5和iOS6。只有iOS6设备失败。
gethostbyname()有什么区别?
或者你有其他解决办法在iOS6中获取本地IP吗?

Notice that, simulator works for both iOS5 and iOS6. Only iOS6 device failed. What's the difference for the gethostbyname()? Or do you have any other solution to get local ip in iOS6?

推荐答案

有几个可能问题:


  • 也许你有一个IPv6地址( gethostbyname()仅适用于IPv4),

  • 或从主机名到IP地址的名称解析无法正常工作。

  • Perhaps you have an IPv6 address (gethostbyname() works only with IPv4),
  • or the name resolution from the hostname to IP address does not work correctly.

以下代码将所有本地地址作为字符串数组返回。它不依赖于名称解析并适用于IPv4和IPv6地址。

The following code returns all local addresses as an array of strings. It does not depend on name resolution and works with both IPv4 and IPv6 addresses.

#include <sys/types.h>
#include <sys/socket.h>
#include <ifaddrs.h>
#include <net/if.h>
#include <netdb.h>

// return all local IP addresses
- (NSArray *)localIPAddresses
{
    NSMutableArray *ipAddresses = [NSMutableArray array] ;

    struct ifaddrs *allInterfaces;

    // Get list of all interfaces on the local machine:
    if (getifaddrs(&allInterfaces) == 0) {
        struct ifaddrs *interface;

        // For each interface ...
        for (interface = allInterfaces; interface != NULL; interface = interface->ifa_next) {
            unsigned int flags = interface->ifa_flags;
            struct sockaddr *addr = interface->ifa_addr;

            // Check for running IPv4, IPv6 interfaces. Skip the loopback interface.
            if ((flags & (IFF_UP|IFF_RUNNING|IFF_LOOPBACK)) == (IFF_UP|IFF_RUNNING)) {
                if (addr->sa_family == AF_INET || addr->sa_family == AF_INET6) {

                    // Convert interface address to a human readable string:
                    char host[NI_MAXHOST];
                    getnameinfo(addr, addr->sa_len, host, sizeof(host), NULL, 0, NI_NUMERICHOST);

                    [ipAddresses addObject:[[NSString alloc] initWithUTF8String:host]];
                }
            }
        }

        freeifaddrs(allInterfaces);
    }
    return ipAddresses;
}

这篇关于gethostbyname()在iOS6中失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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