我正在研究项目,它找到了在LAN网络中连接的摄像机IP地址,我正在使用局域网搜索iOS来查找摄像机,它可以工作但有时相机已经自我分配到( 169.254.164.XXX)默认IP地址。局域网搜索无法找到它。如何在LAN网络中找到所有相机,即使相机处于默认网络中?
答案 0 :(得分:0)
这将返回本地IP地址:
- (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 == 2) {
// Check if interface is end 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;
}