views:

1704

answers:

2

I'm programming in objective-C for several iPod devices and I was wondering about something. I'm developing an application that utilizes the server-client model and I'm using the UDP protocol with C sockets. Is there a class out there that allows me to determine the iPod devices IP address? After googling around other forums, I haven't found anything. Obviously this command wouldn't work, but something like ipAddress = self.ip is what I had in mind. I'm setting up multicast C sockets and I'm trying to do a workaround that resembles the ping command, which obviously doesn't exist in objective-C either or to my knowledge (which is limited, as I've only been programming in objective-C since the start of this summer) at least. Any advice or tips?

A: 

Did you see this? http://www.appsamuck.com/day4.html. I think the right answer is to use CFHost in the SDK.

EDIT
It appears the source in that project is using the following code, which makes it a completely invalid solution unless Apple decides to put NSHost into the SDK.

-(NSString*)getAddress {  
    char iphone_ip[255];  
    strcpy(iphone_ip,"127.0.0.1"); // if everything fails  
    NSHost* myhost =[NSHost currentHost];  
    if (myhost)  
    {  
        NSString *ad = [myhost address];  
        if (ad)  
            strcpy(iphone_ip,[ad cStringUsingEncoding: NSISOLatin1StringEncoding]);  
    }  
    return [NSString stringWithFormat:@"%s",iphone_ip];   
}
marcc
This is the first thing that popped into my head, as well
ryansstack
NSHost is not supported by the iPhone SDK. It would be great if it were though.
Josh Bradley
NSHost is supported in iPhone SDK!
NSHost is supported by the iPhone SDK!
yakovlev
+2  A: 

This snippet of code will retrieve it by looping through the interfaces.

- (NSString *)getIPAddress 
{
    NSString *address = @"error";
    struct ifaddrs *interfaces = NULL;
    struct ifaddrs *temp_addr = NULL;
    int success = 0;

    // retrieve the current interfaces - returns 0 on success
    success = getifaddrs(&interfaces);
    if (success == 0)  
    {
     // Loop through linked list of interfaces
     temp_addr = interfaces;
     while(temp_addr != NULL)  
     {
      if(temp_addr->ifa_addr->sa_family == AF_INET)
      {
       // Check if interface is en0 which is the wifi connection on the iPhone  
       if([[NSString stringWithUTF8String:temp_addr->ifa_name] isEqualToString:@"en0"])  
       {
        // Get NSString from C String
        address = [NSString stringWithUTF8String:inet_ntoa(((struct sockaddr_in *)temp_addr->ifa_addr)->sin_addr)];
       }
      }
      temp_addr = temp_addr->ifa_next;
     }
    }

    // Free memory
    freeifaddrs(interfaces); 
    return address; 
}
byte
First test and run the code , then post the full code.i am getting warings and error from the above code.warning: implicit declaration of function 'getifadrs'error: dereferencing pointter to incomplete type for (temp_addr != NULL)and few more errors.
Biranchi