从主机名获得服务器IP地址(Obtaining a server IP address from hostname)

编程入门 行业动态 更新时间:2024-10-27 10:25:31
主机名获得服务器IP地址(Obtaining a server IP address from hostname)

在对主机名执行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);

更多推荐

本文发布于:2023-07-23 04:41:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1227571.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:主机名   地址   服务器   IP   hostname

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!