Tuesday, 27 August 2013

How to fetch a server's hostname in a LAN

How to fetch a server's hostname in a LAN

i have a server in a LAN EX:ip http://192.168.12.8:8080/server/ <--- i
want this HostName
i used iPhone/iPad: How to get my IP address programmatically?
its all ok,i can fetch my iphone LAN ip:http://192.168.12.2
and i fetch LAN HostName use GCD
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0),
^{
NSString *iphoneLANIP = [self localIPAddress];//192.168.12.2
int i = [iphoneLANIP length];
NSRange range = NSMakeRange(8, i-8);//192.168. ---
NSString *aString = [iphoneLANIP substringWithRange:range];
NSString *deleteString = [aString substringFromIndex:[aString
rangeOfString:@"."].location+1];
int deleteI = [deleteString length];
//newIP = 192.168.12.
NSString *newIP = [iphoneLANIP substringToIndex:iphoneLANIP.length -
deleteI];
for (int x = 1 ; x <= 255 ; x ++){
NSURL *url = [NSURL URLWithString:[NSString
stringWithFormat:@"http://%@%d:8080/server/",newIP,x]];
NSString *urlString = [NSString stringWithContentsOfURL:url
encoding:NSUTF8StringEncoding error:nil];
if (urlString != nil){
NSLog(@"hostname ok %@",url);
break;
}else{
NSLog(@"hostname no %@",url);
}
}
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"IP OK");
});
});
i can find my server address :http://192.168.12.8:8080/server/
but I found that I need to use 5 minutes, or longer
how to faster fetch a server's hostname in a LAN?

No comments:

Post a Comment