从主机名获取服务器IP地址 [英] Obtaining a server IP address from hostname

查看:151
本文介绍了从主机名获取服务器IP地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当对主机名执行 NSURLRequest 时,是否可以获取响应来自的服务器的IP地址?

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

NSURL 方法:

- (NSString *)host;

只返回主机名,我看不到从任何其他 NSURL 方法。

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

也许有一种方法可以在启动 NSURLRequest

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

推荐答案

示例代码

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

这篇关于从主机名获取服务器IP地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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