• <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)論   


            只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。
            網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問(wèn)   Chat2DB   管理


            公告

            Locations of visitors to this page

            導(dǎo)航

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

            統(tǒng)計(jì)

            常用鏈接

            留言簿

            隨筆分類

            隨筆檔案

            文章分類

            文章檔案

            博客收藏

            搜索

            最新評(píng)論

            閱讀排行榜

            評(píng)論排行榜

            久久精品国产精品亚洲人人 | 久久热这里只有精品在线观看| 国产一区二区三区久久| 少妇久久久久久被弄高潮| 久久亚洲AV成人无码| 欧美日韩精品久久免费| 久久综合视频网| 久久婷婷五月综合97色| 无码八A片人妻少妇久久| 精品久久久无码21p发布| 久久亚洲熟女cc98cm| 伊人久久大香线焦AV综合影院 | 人妻无码精品久久亚瑟影视| 国内精品久久久久久久久| 久久久久18| 亚洲人AV永久一区二区三区久久| 伊人久久大香线蕉成人| 99久久夜色精品国产网站| 精品国产乱码久久久久久郑州公司 | 99久久免费只有精品国产| 99久久精品免费看国产| 青春久久| 久久久久亚洲AV无码网站| 色成年激情久久综合| 香蕉久久永久视频| 久久w5ww成w人免费| 久久激情五月丁香伊人| 亚洲综合久久久| 久久se精品一区二区| 午夜精品久久久久久影视777| 亚洲国产美女精品久久久久∴| 91久久九九无码成人网站| 久久婷婷五月综合97色直播| 久久免费高清视频| 久久人人爽人人人人片av| 欧美日韩中文字幕久久伊人| 久久国产亚洲精品| 狠狠精品久久久无码中文字幕| 午夜欧美精品久久久久久久| 国产免费久久久久久无码| 国产三级久久久精品麻豆三级|