#import "ViewController.h"
#include <netdb.h>
#include <arpa/inet.h>
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSString *webSiteString = @"http://www.baidu.com/wa/asfafaf";
//截取host
NSString*str_intercepted = [webSiteString substringFromIndex:7];
NSString*str_character = @"/";
NSRange range= [str_intercepted rangeOfString:str_character];
NSString*str_complete = [str_intercepted substringToIndex:range.location];
NSLog(@"截取出来的字符串=%@",str_complete);
//NSString to char*
const char *webSite = [str_complete cStringUsingEncoding:NSASCIIStringEncoding];
// Get host entry info for given host
struct hostent *remoteHostEnt = gethostbyname(webSite);
// Get address info from host entry
struct in_addr *remoteInAddr = (struct in_addr *) remoteHostEnt->h_addr_list[0];
// Convert numeric addr to ASCII string
char *sRemoteInAddr = inet_ntoa(*remoteInAddr);
//char* to NSString
NSString *ip = [[[NSString alloc] initWithCString:sRemoteInAddr
encoding:NSASCIIStringEncoding] autorelease];
NSLog(@"ip:%@",ip);
}
@end
DNStoIP
最新推荐文章于 2023-07-08 22:40:56 发布