从主机名获取服务器IP地址

5

当使用NSURLRequest请求主机名时,是否可以获取响应来自的服务器的IP地址?

NSURL方法:

- (NSString *)host;

仅返回主机名,我没有看到从任何其他NSURL方法中获取IP地址的方法。

也许在初始化NSURLRequest之前执行主机查找的方法?

3个回答

11
你可以使用系统调用gethostbyname()来解析主机名,然后使用返回的结构体获取IP地址。最后请参考inet_ntop()

示例代码

struct hostent *hostentry;
hostentry = gethostbyname("google.com");
char * ipbuf;
ipbuf = inet_ntoa(*((struct in_addr *)hostentry->h_addr_list[0]));
printf("%s",ipbuf);

2
请注意,inet_ntoa已被弃用,您应该使用inet_ntop来支持IPv6地址。 - Thomas Vervest
如果您想查看所有IP地址,您需要在hostentry->h_addr_list[i]上运行循环以显示它们的全部。 - Mike.R
请使用[getaddrinfo()](https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/NetworkingTopics/Articles/ResolvingDNSHostnames.html)而不是gethostbyname()进行编程。它允许IPV6,并适用于返回多个IP地址的情况。 - JamesWebbTelescopeAlien

2

我有一个关于如何在Unix / Linux中从主机名获取IP的问题,但是发现这个问题是在不同的环境下提出的,我想如果我错了,请纠正我。

由于已经问过了这个问题,所以我担心避免被stackoverflow团队标记为重复提问。

问题:如何在Unix / Linux中从主机名获取IP?

答案:存在两个命令:

  1. ping host_name

例如:

ping -s google.co.in
PING google.co.in: 56 data bytes
64 bytes from dfw06s48-in-f3.1e100.net (216.58.194.99): icmp_seq=0. time=2.477 ms
64 bytes from dfw06s48-in-f3.1e100.net (216.58.194.99): icmp_seq=1. time=1.415 ms
64 bytes from dfw06s48-in-f3.1e100.net (216.58.194.99): icmp_seq=2. time=1.712 ms
  1. nslookup 主机名

例如:

nslookup google.co.in
Server:         155.179.59.249
Address:        155.179.59.249#53

Non-authoritative answer:
Name:   google.co.in
Address: 216.58.194.99

0
#import <arpa/inet.h>

- (BOOL)resolveHost:(NSString *)hostname {
    Boolean result;
    CFHostRef hostRef;
    CFArrayRef addresses;
    NSString *ipAddress = nil;
    hostRef = CFHostCreateWithName(kCFAllocatorDefault, (__bridge 
    CFStringRef)hostname);
    CFStreamError *error = NULL;
    if (hostRef) {
        result = CFHostStartInfoResolution(hostRef, kCFHostAddresses, error); 
        if (result) {
            addresses = CFHostGetAddressing(hostRef, &result);
        }
    }
    if (result) {
        CFIndex index = 0;
        CFDataRef ref = (CFDataRef) CFArrayGetValueAtIndex(addresses, index);

        int port=0;
        struct sockaddr *addressGeneric;

        NSData *myData = (__bridge NSData *)ref;
        addressGeneric = (struct sockaddr *)[myData bytes];

        switch (addressGeneric->sa_family) {
            case AF_INET: {
                struct sockaddr_in *ip4;
                char dest[INET_ADDRSTRLEN];
                ip4 = (struct sockaddr_in *)[myData bytes];
                port = ntohs(ip4->sin_port);
                ipAddress = [NSString stringWithFormat:@"%s", inet_ntop(AF_INET, &ip4->sin_addr, dest, sizeof dest)];
        }
            break;

        case AF_INET6: {
            struct sockaddr_in6 *ip6;
            char dest[INET6_ADDRSTRLEN];
            ip6 = (struct sockaddr_in6 *)[myData bytes];
            port = ntohs(ip6->sin6_port);
            ipAddress = [NSString stringWithFormat:@"%s", inet_ntop(AF_INET6, &ip6->sin6_addr, dest, sizeof dest)];
        }
            break;
        default:
            ipAddress = nil;
            break;
        }
    }
    NSLog(@"%@", ipAddress);
    if (ipAddress) {
        return YES;
    } else {
        return NO;
    }
}

[self resolveHost:@"google.com"]

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接