• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            brucejini

            iphone DNS解析和網絡測試

            #import  <CFNetwork/CFHost.h>
            #import <netinet/in.h>
            #import <netdb.h> 
            #import <SystemConfiguration/SystemConfiguration.h>

            #pragma mark 
            -
            #pragma mark DNS解析和網絡測試
            -(NSString*) getAddressFromArray:(CFArrayRef) addresses
            {
                
            struct sockaddr  *addr;
                
            char             ipAddress[INET6_ADDRSTRLEN];
                CFIndex          index, count;
                
            int              err;
                
                assert(addresses 
            != NULL);
                
                
                count 
            = CFArrayGetCount(addresses);
                
            for (index = 0; index < count; index++) {
                    addr 
            = (struct sockaddr *)CFDataGetBytePtr(CFArrayGetValueAtIndex(addresses, index));
                    assert(addr 
            != NULL);
                    
                    
            /* getnameinfo coverts an IPv4 or IPv6 address into a text string. */
                    err 
            = getnameinfo(addr, addr->sa_len, ipAddress, INET6_ADDRSTRLEN, NULL, 0, NI_NUMERICHOST);
                    
            if (err == 0) {
                        NSLog(
            @"解析到ip地址:%s\n", ipAddress);
                    } 
            else {
                        NSLog(
            @"地址格式轉換錯誤:%d\n", err);
                    }
                }
                
            return    [[[NSString alloc] initWithFormat:@"%s",ipAddress] autorelease];//這里只返回最后一個,一般認為只有一個地址
            }


            -(bool)getReachability:(CFDataRef) data withNameOrAddress:(CFStringRef) nameOrAddress
            {
                SCNetworkConnectionFlags  
            *flags;
                CFIndex                   length;
                
            char                      *input;
                Boolean                   success;
                
                assert(data 
            != NULL);
                assert(nameOrAddress 
            != NULL);
                
                
            /* CFStringGetMaximumSizeForEncoding determines max bytes a string of specified length will take up if encoded. */
                length 
            = CFStringGetMaximumSizeForEncoding(CFStringGetLength(nameOrAddress), kCFStringEncodingASCII);
                input 
            = malloc(length + 1);
                assert(input 
            != NULL);
                
                success 
            = CFStringGetCString(nameOrAddress, input, length + 1, kCFStringEncodingASCII);
                assert(success);
                
                flags 
            = (SCNetworkConnectionFlags *)CFDataGetBytePtr(data);
                assert (flags 
            != NULL);
                
                
            /* If you only have a PPP interface enabled, the flags will be 0 because of a bug. <rdar://problem/3627771> */
                
            if (*flags == 0) NSLog(@"%s -> Reachability Unknown\n", input);
                
                
            if (*flags & kSCNetworkFlagsTransientConnection)  NSLog(@"%s -> Transient Connection\n",  input);
                
            if (*flags & kSCNetworkFlagsReachable)           {
                    NSLog(
            @"%s -> Reachable\n",             input);
                    success 
            = YES;
                }
            else {
                    success 
            = NO;
                }
                
            if (*flags & kSCNetworkFlagsConnectionRequired)   NSLog(@"%s -> Connection Required\n",   input);
                
            if (*flags & kSCNetworkFlagsConnectionAutomatic)  NSLog(@"%s -> Connection Automatic\n",  input);
                
            if (*flags & kSCNetworkFlagsInterventionRequired) NSLog(@"%s -> Intervention Required\n", input);
                
            if (*flags & kSCNetworkFlagsIsLocalAddress)       NSLog(@"%s -> Is Local Address\n",      input);
                
            if (*flags & kSCNetworkFlagsIsDirect)             NSLog(@"%s -> Is Direct\n",             input);
                
                free(input);
                
            return success;
            }

            -(void) serverResoluton{

                CFStringRef             hostName 
            = (CFStringRef)self.serverInfo.serverAddress; 
                CFHostRef            host;
                CFStreamError        error;
                Boolean              success;
                CFArrayRef             addressArray;
                CFDataRef             ReachableData;
                
                assert(hostName 
            != NULL);
                
                
            /* Creates a new host object with the given name. */
                host 
            = CFHostCreateWithName(kCFAllocatorDefault, hostName);
                assert(host 
            != NULL);
                
                success 
            = CFHostStartInfoResolution(host, kCFHostAddresses, &error);
                
            if (!success) {
                    NSLog(
            @"CFHostStartInfoResolution 返回錯誤 (%d, %ld)", error.domain, error.error);//如果解析地址失敗,使用直接指定IP
                    NSLog(@"啟用直接指定IP:%@",self.serverInfo.serverIPAddress);
                    [self.serverInfo.serverAddress release];
                    self.serverInfo.serverAddress 
            = self.serverInfo.serverIPAddress;
                }
            else {
                    addressArray 
            = CFHostGetAddressing(host, nil);
                    [self.serverInfo.serverAddress release];
                    self.serverInfo.serverAddress 
            = [[NSString alloc] initWithFormat:@"%@",[self getAddressFromArray:addressArray]];
                    NSLog(
            @"替換地址為:%@", self.serverInfo.serverAddress);
                }
                
                
            //使用新地址來確認可連接性
                hostName = (CFStringRef)self.serverInfo.serverAddress;
                host 
            = CFHostCreateWithName(kCFAllocatorDefault, hostName);
                success 
            = CFHostStartInfoResolution(host, kCFHostReachability, &error);
                
            if (!success) {
                    NSLog(
            @"CFHostStartInfoResolution 返回錯誤 (%d, %ld)", error.domain, error.error);
                    
            //暫不知到這里會在什么情況下發生
                }else {
                    ReachableData 
            = CFHostGetReachability(host, nil);
                    success 
            = [self getReachability:ReachableData withNameOrAddress:(CFStringRef)hostName];
                    
            if (!success) {
                        [self.serverInfo.serverAddress release];
                        self.serverInfo.serverAddress 
            = self.serverInfo.serverAddressBak;//在這里添加備用服務器
                    }
                }    

            }

            posted on 2011-01-27 15:43 路人甲 閱讀(4850) 評論(1)  編輯 收藏 引用

            評論

            # re: iphone DNS解析和網絡測試 2016-06-03 09:06 伍林華

            沒有serverInfo這個屬性吧?這里總是報錯!  回復  更多評論   

            公告

            Locations of visitors to this page

            導航

            <2016年6月>
            2930311234
            567891011
            12131415161718
            19202122232425
            262728293012
            3456789

            統計

            常用鏈接

            留言簿

            隨筆分類

            隨筆檔案

            文章分類

            文章檔案

            博客收藏

            搜索

            最新評論

            閱讀排行榜

            評論排行榜

            久久国产香蕉视频| 日韩AV毛片精品久久久| 粉嫩小泬无遮挡久久久久久| 久久国产高清一区二区三区| www久久久天天com| 久久久久久国产精品无码下载| 久久久久久久亚洲精品| 日韩精品久久无码中文字幕| 99久久国产亚洲综合精品| 国产 亚洲 欧美 另类 久久| 久久精品国产免费| www.久久热.com| 99久久免费国产精品热| 午夜精品久久久久久影视riav| 国产精品伦理久久久久久 | 久久精品久久久久观看99水蜜桃| 天天久久狠狠色综合| 久久国产精品77777| 久久精品国产清高在天天线| 久久精品中文字幕一区| 久久久青草久久久青草| 日本人妻丰满熟妇久久久久久| 午夜精品久久久久久| 久久成人精品| 久久不见久久见免费影院www日本| 日韩久久久久久中文人妻| 久久天天躁狠狠躁夜夜不卡| 久久激情亚洲精品无码?V| 伊人丁香狠狠色综合久久| 久久午夜福利电影| 无码人妻少妇久久中文字幕蜜桃| 久久综合狠狠综合久久综合88| 一本久久综合亚洲鲁鲁五月天亚洲欧美一区二区 | 久久久久亚洲精品男人的天堂| 97精品国产91久久久久久| 久久精品中文无码资源站| 久久综合狠狠综合久久| 久久精品中文无码资源站| 久久国产精品99久久久久久老狼| 国产成年无码久久久久毛片| 97久久精品人妻人人搡人人玩|