DNStoIP

来源:互联网 发布:淘宝手表外贸店推荐 编辑:程序博客网 时间:2024/06/05 03:24
#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