可以使用SystemConfiguration获取IP的主机名吗? [英] Possible to get the hostname of an IP using SystemConfiguration?

查看:109
本文介绍了可以使用SystemConfiguration获取IP的主机名吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用SystemConfiguration框架从我应用程序中的动态存储中获取当前IP.

I'm using the SystemConfiguration framework to get the current IP from the dynamic store in my app.

是否也可以从动态存储中获取主机名?当前的IP可能是本地IP,所以我试图将返回的值设置为"mymac.local"或"2adg3.dsl.lgtpmi.sbcglobal.net",具体取决于我与网络的连接方式.

Is it possible to get the hostname from the dynamic store as well? The current IP could be local, so I'm trying to get the returned value to be something like "mymac.local" or "2adg3.dsl.lgtpmi.sbcglobal.net", depending on how I'm connected to the net.

我尝试了SCDynamicStoreCopyLocalHostName(NULL),但即使IP解析为互联网上每个人都可以访问的主机名,它也仅返回计算机名本身(而不包含.local部分).

I tried SCDynamicStoreCopyLocalHostName(NULL), but it only returns the computer name itself (and not with the .local part), even if the IP resolves to a hostname that's accessible to everyone on the internet.

推荐答案

您可以尝试

[[NSHost currentHost] names]

我刚刚尝试了它,它返回了我机器的所有本地主机名.

I just tried it and it returns all local hostnames of my machine.

更新:
这是一篇很好的博客文章,修正了一些NSHost问题:
http://cocoawithlove.com/2009/11/drop-in-fix-for-problems-with-nshost.html

Update:
This is a good blog post that fixes some NSHost issues:
http://cocoawithlove.com/2009/11/drop-in-fix-for-problems-with-nshost.html

这篇关于可以使用SystemConfiguration获取IP的主机名吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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