Re: Get Ip Addresses
Re: Get Ip Addresses
- Subject: Re: Get Ip Addresses
- From: Jonathan Wight <email@hidden>
- Date: Mon, 5 Jan 2004 14:33:21 -0600
Here's some code that I used. [NSHost hostWithSockAddr:] is category
method for NSHost that merely sucks the IP address from a sockaddr_in
structure.
Jon.
- (NSDictionary *)hostsForAllInetInterfaces
{
NSMutableDictionary *theDictionary = [NSMutableDictionary dictionary];
//
struct ifaddrs *theInterfaces = NULL;
int theResult = getifaddrs(&theInterfaces);
if (theResult != 0)
[NSException raiseStdLibError:@"-[XXX hostsForAllInetInterfaces]
getifaddrs failed."];
struct ifaddrs *theInterface = theInterfaces;
while (theInterface != NULL)
{
// NSLog(@"%s (0x%X %d)", theInterface->ifa_name,
theInterface->ifa_flags, theInterface->ifa_addr->sa_family);
if (theInterface->ifa_addr->sa_family == AF_INET)
{
NSHost *theHost = [NSHost hostWithSockAddr:(struct sockaddr_in
*)theInterface->ifa_addr];
[theDictionary setObject:theHost forKey:[NSString
stringWithCString:theInterface->ifa_name]];
}
//
theInterface = theInterface->ifa_next;
}
freeifaddrs(theInterfaces);
//
return(theDictionary);
}
On Jan 05, 2004, at 14:12, Allen Thomas wrote:
>
Can someone help me. I am trying to develop a class in Objective C
>
that would be able to retrieve the network addresses for all the
>
interfaces currently connected. Much like ifconfig does on the
>
console. I have looked at a few implementations and could not seem to
>
be able to use them in my xcode project correctly. Any help would be
>
great.
>
>
Allen Thomas
[demime 0.98b removed an attachment of type application/pkcs7-signature which had a name of smime.p7s]
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.