在IPhone上获取DNS服务器IP [英] Getting DNS server IP on IPhone
问题描述
我想使用 libresolv
框架获得我的dns服务器IP,但我得到的是0.0.0.0
- (void)getDns
{
res_init();
for(int i = 0; i {
printf(%s,inet_ntoa(_res.nsaddr_list [i] .sin_addr));
}
}
我的代码在这里有什么问题?
感谢
编辑
$ b b
尝试在SO的其他问题中使用代码,但这是我在主机是apple.com时得到的
2011-03-16 15:07:21.689 MobileMax [5876:207]已解决0:< 17.149.160.49>
2011-03-16 15:07:21.691 MobileMax [5876:207]分辨率1:< 17.251.200.70>
2011-03-16 15:07:21.691 MobileMax [5876:207]分辨率2:< 17.112.152.57>
运行命令时: cat /etc/resolv.conf
我得到 nameserver 10.0.0.138
这是我想要的。
我也试过这个,但仍然得到0.0.0.0响应。
u_char buf [NS_PACKETSZ];
int responseLen;
res_query(www.google.com,ns_c_in,ns_t_ns,buf,sizeof(buf));
获取DNS服务器地址。不要忘记链接libresolv.lib
#include< arpa / inet.h> ;
#include< ifaddrs.h>
#include< resolv.h>
#include< dns.h>
- (NSString *)getDNSServers
{
//不要忘记链接libresolv.lib
NSMutableString * addresses = [[NSMutableString alloc] initWithString:@DNS地址\\\
];
res_state res = malloc(sizeof(struct __res_state));
int result = res_ninit(res);
if(result == 0)
{
for(int i = 0; i< res-> nscount; i ++)
{
NSString * s = [NSString stringWithUTF8String:inet_ntoa(res-> nsaddr_list [i] .sin_addr)];
[addresses appendFormat:@%@ \\\
,s];
NSLog(@%@,s);
}
}
else
[addresses appendString:@res_init result!= 0];
返回地址;
}
I'm trying to get the my dns server IP using using libresolv
framework , but all I get is "0.0.0.0"
-(void) getDns
{
res_init();
for (int i=0;i<MAXNS;i++)
{
printf("%s",inet_ntoa(_res.nsaddr_list[i].sin_addr));
}
}
Anything wrong with my code here ?
Thanks
Edit
Tried to use the code in SO's other question, but this is what I get when the host is "apple.com"
2011-03-16 15:07:21.689 MobileMax[5876:207] RESOLVED 0:<17.149.160.49>
2011-03-16 15:07:21.691 MobileMax[5876:207] RESOLVED 1:<17.251.200.70>
2011-03-16 15:07:21.691 MobileMax[5876:207] RESOLVED 2:<17.112.152.57>
While when running the command : cat /etc/resolv.conf
I get nameserver 10.0.0.138
which is what I want.
Any idea how to get that ?
Edit2
I tried this as well, but still getting the "0.0.0.0" response.
u_char buf[NS_PACKETSZ];
int responseLen;
res_query("www.google.com",ns_c_in,ns_t_ns,buf,sizeof(buf));
Try below code to get DNS Server Address . Don't forget to link libresolv.lib
#include <arpa/inet.h>
#include <ifaddrs.h>
#include <resolv.h>
#include <dns.h>
- (NSString *) getDNSServers
{
// dont forget to link libresolv.lib
NSMutableString *addresses = [[NSMutableString alloc]initWithString:@"DNS Addresses \n"];
res_state res = malloc(sizeof(struct __res_state));
int result = res_ninit(res);
if ( result == 0 )
{
for ( int i = 0; i < res->nscount; i++ )
{
NSString *s = [NSString stringWithUTF8String : inet_ntoa(res->nsaddr_list[i].sin_addr)];
[addresses appendFormat:@"%@\n",s];
NSLog(@"%@",s);
}
}
else
[addresses appendString:@" res_init result != 0"];
return addresses;
}
这篇关于在IPhone上获取DNS服务器IP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!