iphone get 3G DNS host name and ip address

为君一笑 提交于 2019-12-18 04:20:37

问题


Is it possible in Objective C / C / C++ to get the current DNS server ip address for 3G / Cell data connection?

Looked around and could not find definitive answer.


回答1:


#include <arpa/inet.h>
#include <ifaddrs.h>
#include <resolv.h>
#include <dns.h>

// 
- (NSString *) getDNSServers 
{
    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;
    }


来源:https://stackoverflow.com/questions/10999612/iphone-get-3g-dns-host-name-and-ip-address

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!