• <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久久这里只有精品| 久久国产精品偷99| 久久青青色综合| 久久电影网2021| 一本大道久久香蕉成人网| 99久久成人国产精品免费| 91精品日韩人妻无码久久不卡| 一本一本久久a久久精品综合麻豆| 久久婷婷五月综合成人D啪 | 亚洲人成无码www久久久| 97精品国产97久久久久久免费| 久久久久综合网久久| 久久成人国产精品免费软件| 日本精品久久久中文字幕| 久久久精品久久久久影院| 久久99精品国产| 久久久女人与动物群交毛片| 久久久午夜精品| 久久久久久亚洲精品不卡| 日本一区精品久久久久影院| 欧美日韩精品久久久久| 国产亚洲精午夜久久久久久| 国产精品美女久久久| 久久er99热精品一区二区| 国产精品久久新婚兰兰| 久久香蕉国产线看观看猫咪?v| 四虎国产永久免费久久| 日本精品久久久久中文字幕8| 久久Av无码精品人妻系列| 亚洲国产另类久久久精品黑人| 久久只有这精品99| 三级片免费观看久久| 中文字幕久久亚洲一区| 亚洲日本久久久午夜精品| 国产精品久久久久蜜芽| 亚洲伊人久久大香线蕉综合图片| 久久综合亚洲鲁鲁五月天| 91麻豆国产精品91久久久| 精品久久久久久久国产潘金莲|