码迷,mamicode.com
首页 > 移动开发 > 详细

iOS域名解析

时间:2015-06-11 17:04:12      阅读:157      评论:0      收藏:0      [点我收藏+]

标签:ios   域名解析   

如何在iOS下进行域名解析

//根据域名获取ip地址
-(NSString*)getIPWithHostName:(const NSString*)hostName
{
    const char *hostN= [hostName UTF8String];
    struct hostent* phot;
    
    @try {
        phot = gethostbyname(hostN);
        
    }
    @catch (NSException *exception) {
        return nil;
    }
    
    struct in_addr ip_addr;
    memcpy(&ip_addr, phot->h_addr_list[0], 4);
    char ip[20] = {0};
    inet_ntop(AF_INET, &ip_addr, ip, sizeof(ip));
    
    NSString* strIPAddress = [NSString stringWithUTF8String:ip];
    return strIPAddress;
}

前提需导入头文件

#include <netdb.h>
#include <sys/socket.h>
#include <arpa/inet.h>

使用

    NSString *string = [self getIPWithHostName:@"www.baidu.com"];
    NSLog(@"%@",string);


iOS域名解析

标签:ios   域名解析   

原文地址:http://blog.csdn.net/zhwezhwe/article/details/46457807

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!