ios-How do I use Apple's 0 configuration network protocol bonjour?

Source: Internet
Author: User

1. PrefaceThis time in order to solve the company app network offline demand, did an Apple launched 0 Configuration network Protocol Bonjour test, mainly in order to solve the iOS device IP acquisition, can be used before the socket broadcast to achieve, but thesimpler and more stable than Apple-launched Bonjor。 Hope to be a little help to everyone, if there is any error is also welcome to point out that we learn from each other. This is a blog--socket broadcast about the socket that I wrote before.implementation of broadcast mechanism of ios-Mobile socket UDP protocol2. What is Bonjour?  What can I do? I believe I have not known Bonjour friend to this thing will be more unfamiliar, bonjor as the title said, is Apple quit a Zero Configuration network protocol, Bonjour can complete the work is mainly in the absence of a central server in the case of IP acquisition, name resolution and service discovery of these three issues. 2.1 IP AccessIn the traditional network environment, the IP address of the device is obtained in two ways, one is static configuration, the device is assigned an IP address by hand, one is dynamic configuration, the device obtains the dynamic IP address through the router's DHCP service. In a server-free network environment, there is no central server to provide DHCP services, the user manually configure the IP address is also inconvenient, this requires a new way to help the device to obtain an IP address, that is, it is hoped that the device can proactively assign itself an available IP address. In IPV6 Environment, the IPV6 protocol itself provides the ability of the device to specify the IP address, so the implementation is very simple, directly using the IPV6 protocol support is possible. In the IPV4 environment, the Bonjour uses a random IP address method, first for the device randomly assigned an IP address belonging to the local network segment, and then check if there is a local conflict, if there is a conflict, randomly generate another new IP address until the available IP address is found. 2.2 Name AnalysisIn the traditional network environment, the correspondence between the name and IP address is resolved through the DNS service. When a device needs to access a domain name, such as "www.saup.com", the device sends "www.saup.com" to the DNS server,The server returns the IP address of the domain name, and the device then accesses the destination server using the returned IP address. In a network environment without a hub server, there is no DNS server to provide the domain name resolution service, and name resolution becomes a serious problem. In response to this problem, the industry's solution is MDNs, called "Multicast DNS", defined in standard document RFC6762. The principle of "Multicast DNS" is simple,when a device needs to parse a name, such as "abc.local.", this device broadcasts a message to all devices on the local network through the UDP protocol, asking who is "abc.local", if a device in the local network thinks it is "abc.local", It gives a response and says its own IP address. Because "Multicast DNS" is based on the UDP protocol, a broadcast message is used, so a central server is not required to provide the DNS resolution service to complete the local name resolution. Bonjour is also based on the MDNs protocol, but Bonjour expands on the MDNs protocol, enhancing the device's ability to respond to "multicast DNS" requests. Under the Bonjour protocol, the application only needs to register a name, so that the response to the "Multicast DNS" request is processed at the bottom. This means that under the Bonjour protocol, the application does not need to listen for "Multicast DNS" requests from the local network and responds, which is done by the underlying system. To differentiate between global domain names and local domain names, the MDNs protocol enableswith ". Local."As the root domain name of the local domain name. 2.3 Service DiscoveryWhen a service provider obtains an IP address and assigns a domain name itself, it is still unable to meet the user's needs. Because the user needs some kind of service, such as a print service, a Web service, the user does not care about the server name and its IP address for those services. To make it easier for users to discover the various services on the local network, Bonjour provides the ability to discover services for the device.The "service discovery" capability provided by Bonjour is based on a simple and straightforward provision that the service provider registers the service as follows: "name. Type of service. The transport protocol type. Local.", for example: "Damonwebserver._http._ Tcp.local. "or" dummieswebserver._http._tcp.local. " This way, when a device uses the HTTP service that it wants to find, Bonjour will look for a service that is registered on the local network that contains "_http", and then return the result to the user's choice. At this point the user is facing the "damonwebserver" and "Dummieswebserver", the user can not care about the two Web services exactly on that device, the device's IP address is what.3.Bonjour of Use? the use of Bonjour is mainly divided into two parts3.1. Bonjour Server Registration Service
First we need to use the Nsnetservice class @property (strong,nonatomic) Nsnetservice*netservice in the iOS SDK;
Initializes the service, specifying the domain, type, name, and port of the service   _netservice = [[Nsnetservice alloc] initwithdomain:@ "Local." type:@ "_http._tcp" Damonwebserver "port:5222";
Specify agent [_netservicesetdelegate:self];//Publish registration Service [_netservice Publish];
Nsnetservicedelegate Agent protocol-related agent method use @protocolnsnetservicebrowserdelegate <NSObject> @optional/* Sent to the Nsnetservicebrowser instance ' s delegate before the instance begins a search. The delegate won't receive this message if the instance are unable to begin a search. Instead, the delegate would receive the-netservicebrowser:didnotsearch:message.*/-(void) Netservicebrowserwillsearch :(nsnetservicebrowser*) anetservicebrowser;/* Sent to the Nsnetservicebrowser instance ' s delegate when the instance ' s Previous running search request has stopped.*/-(void) Netservicebrowserdidstopsearch: (nsnetservicebrowser*) anetservicebrowser;/* Sent to the Nsnetservicebrowser instance ' s delegate when a error in searching for Domains or Servic ES has occurred. The error dictionary would contain the Key/value pairs representing the error domain and code (see the Nsnetserviceserror E numeration above for error code constants). It is possible for an error to occur after a search have been started SUCCessfully.*/-(void) Netservicebrowser: (nsnetservicebrowser*) Anetservicebrowser Didnotsearch: (NSDictionary*) errordict;/* Sent to the Nsnetservicebrowser instance ' s delegate for each domain discovered. If There is more domains, morecoming would be YES. If for some reason handling discovered domains requires significant processing, accumulating domains until morecoming are N O and then doing the processing in bulk fashion could be desirable.*/-(void) Netservicebrowser: (nsnetservicebrowser*) Anetser Vicebrowser Didfinddomain: (nsstring*) domainstring morecoming: (BOOL) morecoming;/* Sent to the Nsnetservicebrowser Instance ' s delegate for each service discovered. If There is more services, morecoming would be YES.  If for some reason handling discovered services requires significant processing, accumulating services until morecoming are NO and then doing the processing in bulk fashion could be desirable.*/-(void) Netservicebrowser: (nsnetservicebrowser*) anets Ervicebrowser Didfindservice: (nsnetservice*) ANetService morecoming: (BOOL) morecoming;/* Sent to the Nsnetservicebrowser instance ' s delegate when a previously discovered do Main is no longer available.*/-(void) Netservicebrowser: (nsnetservicebrowser*) Anetservicebrowser Didremovedomain: ( nsstring*) domainstring morecoming: (BOOL) morecoming;/* Sent to the Nsnetservicebrowser instance ' s delegate when a Previously discovered service is no longer published.*/-(void) Netservicebrowser: (nsnetservicebrowser*) Anetservicebrowser Didremoveservice: (nsnetservice*) Anetservice morecoming: (BOOL) morecoming;
3.2. Bonjour Client Discovery Local Service
The client is primarily using Nsnetservicebrowser@property (strong,nonatomic) Nsnetservicebrowser*serverbrowser in the iOS SDK; @property ( strong,nonatomic) Nsmutablearray*servers;//nsnetservice on the client for parsing @property (strong,nonatomic) NSNetService* netserver;//initialize nsnetservicebrowser_serverbrowser= [[nsnetservicebrowseralloc]init];//Designated Proxy _ Serverbrowser.delegate = Self;_servers = [nsmutablearrayarray];// The lookup service then uses the Searchforservicesoftype method of the Nsnetservicebrowser instance to find the service, specifying the type of service to look for and the domain to find [_ serverbrowsersearchforservicesoftype:@ "_http._tcp." indomain:@ "Local."];

Nsnetservicebrowserdelegate Agent protocol Related agent method

Note: The client can resolve the service through Nsnetservice, after the successful resolution, can get the data details of the communication, such as: IP address, port and other information. -//Here are a few common proxy methods
The service will be parsed,-(void) Netservicewillresolve: (Nsnetservice *) Netservice {       NSLog (@ "Netservicewillresolve");   } Resolution Service succeeded-(void) Netservicedidresolveaddress: (Nsnetservice *) Netservice {       NSLog (@ "Service ip:%@,------port:%d", Netservice.addresses,netservice.port);       } Parsing service failure, parsing error-(void) Netservice: (Nsnetservice *) Netservice didnotresolve: (nsdictionary *) errordict {       NSLog (@ " Didnotresolve:%@ ", errordict);   }  Discovered services-(void) Netservicebrowser: (Nsnetservicebrowser *) Netservicebrowser didfindservice: (Nsnetservice *) Netservice morecoming: (BOOL) moreservicescoming{       NSLog (@ "Didfindservice");    _netserver = Netservice;    _netserver.delegate = self;    Set resolution time-out    [_netserverresolvewithtimeout:5.0];}

Clear Saup
Source: http://www.cnblogs.com/qingche/
This article is copyright to the author and the blog Park is shared, welcome reprint, but must retain this paragraph statement, and in the article page obvious location to give the original text connection.

ios-How do I use Apple's 0 configuration network protocol bonjour?

Related Article

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.