Re: Obtaining local host name
Re: Obtaining local host name
- Subject: Re: Obtaining local host name
- From: Josh Graessley <email@hidden>
- Date: Sat, 24 May 2008 21:34:48 -0700
On May 24, 2008, at 5:04 PM, Nathan Kinsinger wrote:
On May 24, 2008, at 4:51 PM, Josh Graessley wrote:
On May 24, 2008, at 3:29 PM, Nathan Kinsinger wrote:
On May 24, 2008, at 3:02 PM, Josh Graessley wrote:
On May 24, 2008, at 1:57 PM, Ryan McGann wrote:
On May 24, 2008, at 12:05 PM, email@hidden
wrote:
gethostname isn't going to work. You're looking for
SCDynamicStoreCopyLocalHostName.
Mark,
I'm curious, why won't gethostname work? The hostname command
seems to return the correct string, and according to the
sources, it simply calls gethostname. What's different about the
SytemConfig call?
gethostname is a legacy API from the days where the host had a
single name. It's obsolete and exists only for compatibility. If
you're using this API in modern code, it's a sign you're probably
doing something wrong.
-josh
Are you talking about all unix networking APIs or just
gethostname. For example getaddrinfo and gethostbyname can return
multiple addresses, should all networking code switch to the apple
APIs?
Just gethostname. getaddrinfo is fine, though it sure would be nice
if there was a cross platform asynchronous version of the API.
And correct me if I'm wrong, but SCDynamicStoreCopyLocalHostName
returns a CFStringRef with a single name, so how is that better?
SCDynamicStoreCopyLocalHostName gives you a very specific name, the
dot-local name. gethostname will return you a name, but it's hard
to know which name or what you can do with that name, where it came
from, or what it means.
Also (for the OP's question) would [[NSHost currentHost] names]
(or name) work?
I have no idea what that API does.
-josh
I just ran a test on my computer
NSLog(@"[[NSHost currentHost] name] = %@", [[NSHost currentHost]
name]);
SCDynamicStoreContext context = { 0, NULL, NULL, NULL };
SCDynamicStoreRef store = SCDynamicStoreCreate(kCFAllocatorDefault,
CFSTR("testStrings"), NULL, &context);
NSLog(@"SCDynamicStoreCopyLocalHostName() = %@",
SCDynamicStoreCopyLocalHostName(store));
and I get
2008-05-24 17:06:26.409 testStrings[95353:10b] [[NSHost
currentHost] name] = BrotherBard.local
2008-05-24 17:06:26.439 testStrings[95353:10b]
SCDynamicStoreCopyLocalHostName() = BrotherBard
It appears that SCDynamicStoreCopyLocalHostName returns the computer
name from the sharing prefs (as Marc Krochmal noted in his post and
which, ultimately, is the same thing... just need to add the .local).
I don't believe that is guaranteed to always be the dot-local name.
-josh
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Macnetworkprog mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden