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
?
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);
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
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With