简体   繁体   中英

Possible to get the hostname of an IP using SystemConfiguration?

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

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.

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.

You could try

[[NSHost currentHost] names]

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM