"IOS" asynchronously resolves DNS

Source: Internet
Author: User

found that the iOS package is a bit disgusting, the select package into the form of Cfrunloop, and the Cfrunloop package into a nsrunloop, and select using the same way. And some convoluted concept, Af_unix encapsulated into the nsport.
The project encountered the need to resolve the problem of DNS asynchronous, the need to resolve DNS, another thread to cancel it, but not a single thread, and then force the end. Such an exit is not graceful.

Finally I wrote a DNS protocol with C ... Want to integrate into the socket cross-platform framework, and later found that I have no way to get DHCP to obtain the DNS server address.
Finally can only be implemented in the upper layer, using the cfnetwork.framework, through the label to cancel

#import @interface tshost:nsobject+ (Nsarray *) getipaddress: (NSString *) host Label: (NSString *) label;+ (void) Cancelparse: (NSString *) label; @end////tshost.m//dns_simple////Created by netriver on 14-5-30.//Copyright (c) 2014 onl Ywish.me. All rights reserved.//#import "TSHost.h" #include #include #include #include #include static Nsmutabledictionary *dic;void host_call_back (cfhostref thehost, Cfhostinfotype typeinfo, const CFStreamError *error, void *info) {} @implementation tshost+ (Nsarray *) getipaddress: (NSString *) host Label: (NSString *) label; {if (dic = = nil) {dic = [nsmutabledictionary dictionary];} if ([dic objectforkey:label]! = nil) {return nil;} Add Cancel interface Nsport *port = [Nsport port]; [DiC Setobject:port Forkey:label]; [[Nsrunloop Currentrunloop] addport:port formode:nsdefaultrunloopmode];//parsing the domain name code cfhostref hostref = Cfhostcreatewithname (NULL, (__bridge cfstringref) (host));//Set Asynchronous Cfhostclientcontext Context;memset (&context,0, sizeof (Cfhostclientcontext)); Cfhostsetclient (Hostref, Host_cAll_back, &context);//Add to Runloopcfhostschedulewithrunloop (Hostref,[[nsrunloop Currentrunloop] getCFRunLoop), Kcfrunloopdefaultmode); NSLog (@ "parsing .....");//Start parsing cfhoststartinforesolution (hostref,kcfhostaddresses,null);//wait for completion [[Nsrunloop Currentrunloop] Acceptinputformode:nsdefaultrunloopmode beforedate:[nsdate distantfuture]];// Cancel Run Loop Cfhostunschedulefromrunloop (Hostref, [[Nsrunloop Currentrunloop] getcfrunloop], kcfrunloopdefaultmode);// Remove cancellation interface [dic Removeobjectforkey:label]; [Port invalidate]; NSLog (@ "Parse complete ...."); Boolean flag;//Gets the resolved IP address Nsarray * array = (__bridge Nsarray *) cfhostgetaddressing (hostref, &flag);// Release the parsed object Cfrelease (HOSTREF); if (flag = = NO) return nil;struct sockaddr_in *sock_ptr; Nsmutablearray * result = [Nsmutablearray array];for (NSData * ipaddr in array) {sock_ptr = (struct-sockaddr_in *) [ipaddr by TES]; NSString * ip = [nsstring stringwithutf8string:inet_ntoa (SOCK_PTR->SIN_ADDR)]; [Result Addobject:ip]; NSLog (@ "IP =========%@", IP);} Cfrelease ((__bridge cftyperef) (array)); return result;} + (void) Cancelparse: (NSString *) label{if (dic = = nil) return; Nsport *theport = [dic objectforkey:label];if (Theport! = nil) {//Cancel run loop [theport sendbeforedate:[nsdate Date] Components: Nil From:nil reserved:0];}} @end

SOURCE Blog Address http://onlywish.me

"IOS" asynchronously resolves DNS

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.