客戶端獲取本地ip地址

- (NSString *)getIPAddressoop

{spa

    NSString *address = @"error";3d

    struct ifaddrs *interfaces = NULL;get

    struct ifaddrs *temp_addr = NULL;string

    int success = 0;it

    

    //retrieve the current interfaces - returns 0 on successio

    success = getifaddrs(&interfaces);error

    if (success == 0) {next

        //Loop through linked list of interfaceswifi

        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);

    NSLog(@"addrees----%@",address);

    return address;

}

相關文章
相關標籤/搜索