Obtaining a server IP address from hostname

心已入冬 提交于 2019-12-30 03:31:24

问题


When performing an NSURLRequest to a hostname, is it possible to obtain the IP address of the server that the response came from?

The NSURL method:

- (NSString *)host;

simply returns the hostname, and I see no way of obtaining the IP address from any of the other NSURL methods.

Perhaps there is a way of performing a host lookup before inititing the NSURLRequest?


回答1:


You can use the system call gethostbyname() to resolve a hostname then use the returning structure to get the ip address. Have a look at inet_ntop() for this last part.

EXAMPLE CODE

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



回答2:


I was asking a question regarding

"how to get IP from hostname in unix\linux?"

but found this question in a different context which is not for Unix I guess, let me correct if I am wrong

since this question already been asked so I am fearing to avoid asking the same question marked as duplicated by stack overflow team.

Quest: how to get IP from hostname in unix\linux?

Ans: the two commands over there

  1. ping host_name

Ex:

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 host_name

Ex:

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



回答3:


#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"]


来源:https://stackoverflow.com/questions/9567521/obtaining-a-server-ip-address-from-hostname

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!