• <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解析和網(wǎng)絡(luò)測(cè)試

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

            #pragma mark 
            -
            #pragma mark DNS解析和網(wǎng)絡(luò)測(cè)試
            -(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(
            @"地址格式轉(zhuǎn)換錯(cuò)誤:%d\n", err);
                    }
                }
                
            return    [[[NSString alloc] initWithFormat:@"%s",ipAddress] autorelease];//這里只返回最后一個(gè),一般認(rèn)為只有一個(gè)地址
            }


            -(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 返回錯(cuò)誤 (%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);
                }
                
                
            //使用新地址來(lái)確認(rèn)可連接性
                hostName = (CFStringRef)self.serverInfo.serverAddress;
                host 
            = CFHostCreateWithName(kCFAllocatorDefault, hostName);
                success 
            = CFHostStartInfoResolution(host, kCFHostReachability, &error);
                
            if (!success) {
                    NSLog(
            @"CFHostStartInfoResolution 返回錯(cuò)誤 (%d, %ld)", error.domain, error.error);
                    
            //暫不知到這里會(huì)在什么情況下發(fā)生
                }else {
                    ReachableData 
            = CFHostGetReachability(host, nil);
                    success 
            = [self getReachability:ReachableData withNameOrAddress:(CFStringRef)hostName];
                    
            if (!success) {
                        [self.serverInfo.serverAddress release];
                        self.serverInfo.serverAddress 
            = self.serverInfo.serverAddressBak;//在這里添加備用服務(wù)器
                    }
                }    

            }

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

            評(píng)論

            # re: iphone DNS解析和網(wǎng)絡(luò)測(cè)試 2016-06-03 09:06 伍林華

            沒(méi)有serverInfo這個(gè)屬性吧?這里總是報(bào)錯(cuò)!  回復(fù)  更多評(píng)論   

            公告

            Locations of visitors to this page

            導(dǎo)航

            <2025年5月>
            27282930123
            45678910
            11121314151617
            18192021222324
            25262728293031
            1234567

            統(tǒng)計(jì)

            常用鏈接

            留言簿

            隨筆分類

            隨筆檔案

            文章分類

            文章檔案

            博客收藏

            搜索

            最新評(píng)論

            閱讀排行榜

            評(píng)論排行榜

            日本精品久久久中文字幕| 99久久婷婷免费国产综合精品| 久久午夜伦鲁片免费无码| 亚洲欧美成人久久综合中文网| 国产日韩久久免费影院| 精品久久久久久久| 韩国三级大全久久网站| 国产情侣久久久久aⅴ免费| 久久夜色精品国产欧美乱| 色8久久人人97超碰香蕉987| 人妻少妇久久中文字幕一区二区 | 久久久无码精品亚洲日韩按摩| 97精品依人久久久大香线蕉97| 亚洲综合熟女久久久30p| 久久不见久久见免费视频7| 久久不射电影网| 久久伊人五月天论坛| 久久精品免费一区二区| 精品少妇人妻av无码久久| 91久久精品国产成人久久| 亚洲国产天堂久久久久久| 亚洲va中文字幕无码久久不卡| 97超级碰碰碰久久久久| 久久久久18| 久久精品亚洲日本波多野结衣| 亚洲午夜精品久久久久久人妖| 精品多毛少妇人妻AV免费久久| yy6080久久| 99久久国产综合精品网成人影院| 亚洲人成电影网站久久| 狠狠88综合久久久久综合网 | www久久久天天com| 久久精品国产亚洲Aⅴ蜜臀色欲| yy6080久久| 国产精品久久久天天影视香蕉| 亚洲中文精品久久久久久不卡| 青青草国产精品久久| 亚洲午夜久久久影院| 久久久无码精品亚洲日韩软件| 麻豆亚洲AV永久无码精品久久| 少妇久久久久久被弄到高潮|