• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Get Ip Addresses
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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.

References: 
 >Get Ip Addresses (From: Allen Thomas <email@hidden>)

  • Prev by Date: New to cocoa/database
  • Next by Date: RE: Get Ip Addresses
  • Previous by thread: Get Ip Addresses
  • Next by thread: Re: Get Ip Addresses
  • Index(es):
    • Date
    • Thread