在对主机名执行NSURLRequest ,是否可以获取响应来自的服务器的IP地址?
NSURL方法:
- (NSString *)host;只是返回主机名,我看不到任何其他NSURL方法获取IP地址的方法。
也许有一种在启动NSURLRequest之前执行主机查找的方法?
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?
最满意答案
您可以使用系统调用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);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);更多推荐
发布评论