iOS开发之-- DNS解析(网络切换的问题解决)
2016-01-04 17:47
916 查看
上次提到过由于电信的问题需要自己手动去解析dns,这里介绍的是如何拦截
每一个请求做解析,但是没有说具体的解析方法,下面简单的记录一下:
res_query方法
这是比较常见的系统调用,使用该方法的时候需要在Xcode中添加libresolv.dylib,然后包含resolv.h头文件即可,具体代码如下:
然而该方法有一个问题,在网络从2/3G和WI-FI之间切换的时候,该方法经常不能正常工作,或者需要等待较长的时间,
gethostbyname
具体代码如下:
该方法在碰到切换网络的时候,出现失败的情况比上面的方法好多了,但偶尔也还是会出现,是时候采用苹果自己的方法了。
CFHostStartInfoResolution
具体实现方法如下:
具体的demo可以到这里看看
每一个请求做解析,但是没有说具体的解析方法,下面简单的记录一下:
res_query方法
int res_query(char *domain_name, int class, int type, char *answer_buffer, int answer_buffer_length)
这是比较常见的系统调用,使用该方法的时候需要在Xcode中添加libresolv.dylib,然后包含resolv.h头文件即可,具体代码如下:
unsigned char res[512]; int nBytesRead = 0; //调用系统方法 nBytesRead = res_query("www.baidu.com", ns_c_in, ns_t_a, res, sizeof(res)); ns_msg handle; ns_initparse(res, nBytesRead, &handle); NSMutableArray *ipList = nil; int msg_count = ns_msg_count(handle, ns_s_an); if (msg_count > 0) { ipList = [[NSMutableArray alloc] initWithCapacity:msg_count]; for(int rrnum = 0; rrnum < msg_count; rrnum++) { ns_rr rr; //解析结果 if(ns_parserr(&handle, ns_s_an, rrnum, &rr) == 0) { char ip1[16]; strcpy(ip1, inet_ntoa(*(struct in_addr *)ns_rr_rdata(rr))); NSString *ipString = [[NSString alloc] initWithCString:ip1 encoding:NSASCIIStringEncoding]; if (![ipString isEqualToString:@""]) { //将提取到的IP地址放到数组中 [ipList addObject:ipString]; } } } }
然而该方法有一个问题,在网络从2/3G和WI-FI之间切换的时候,该方法经常不能正常工作,或者需要等待较长的时间,
gethostbyname
struct hostent *gethostbyname(const char *hostName);
具体代码如下:
struct hostent *host = gethostbyname("www.google.com.hk"); struct in_addr **list = (struct in_addr **)host->h_addr_list; //获取IP地址 NSString *ip= [NSString stringWithCString:inet_ntoa(*list[0]) encoding:NSUTF8StringEncoding]; NSLog(@"ip address is : %@",ip);
该方法在碰到切换网络的时候,出现失败的情况比上面的方法好多了,但偶尔也还是会出现,是时候采用苹果自己的方法了。
CFHostStartInfoResolution
Boolean CFHostStartInfoResolution (CFHostRef theHost, CFHostInfoType info, CFStreamError *error);
具体实现方法如下:
Boolean result,bResolved; CFHostRef hostRef; CFArrayRef addresses = NULL; CFStringRef hostNameRef = CFStringCreateWithCString(kCFAllocatorDefault, "www.google.com.hk", kCFStringEncodingASCII); hostRef = CFHostCreateWithName(kCFAllocatorDefault, hostNameRef); if (hostRef) { result = CFHostStartInfoResolution(hostRef, kCFHostAddresses, NULL); if (result == TRUE) { addresses = CFHostGetAddressing(hostRef, &result); } } bResolved = result == TRUE ? true : false; if(bResolved) { struct sockaddr_in* remoteAddr; for(int i = 0; i < CFArrayGetCount(addresses); i++) { CFDataRef saData = (CFDataRef)CFArrayGetValueAtIndex(addresses, i); remoteAddr = (struct sockaddr_in*)CFDataGetBytePtr(saData); if(remoteAddr != NULL) { //获取IP地址 char ip[16]; strcpy(ip, inet_ntoa(remoteAddr->sin_addr)); } } } CFRelease(hostNameRef); CFRelease(hostRef);
具体的demo可以到这里看看
相关文章推荐
- 域名DNS解析的故障解决方法
- iOS开发之路--微博“更多”页面
- python实现dnspod自动更新dns解析的方法
- iOS开发之路--微博骨架搭建
- IOS开发代码分享之获取启动画面图片的string
- IOS开发之路--C语言数组和字符串
- IOS开发代码分享之用nstimer实现倒计时功能
- IOS开发代码分享之设置UISearchBar的背景颜色
- iOS和tvOS游戏按需加载资源简介
- ios开发之环境搭建-01
- 在开发iOS程序时对日期处理的总结
- iOS开发——keychain的使用
- iOS Graphics Technologies iOS 图形技术
- iOS开发异常错误总结
- 简述Windows系统名称解析过程(轉載)
- 学习Objective-C:入门手册
- iOS开发 简单实现视频音频的边下边播
- 初学ios开发之iOS 证书、描述文件、AppID
- iOS开发 — Quartz 2D知识点应用 (制作了一个Demo,源代码)