IOS-how to use Apple's zero-configuration Network Protocol Bonjour ?, Ios-bonjour
1. preface in this period, a Test of the zero-configuration Network Protocol Bonjour launched by Apple was designed to address the offline network demand of the company's apps, mainly to solve the problem of obtaining IP addresses of iOS devices, previously, we could use socket broadcast to achieve this, but Apple's Bonjor is simpler and more stable. I hope it will be helpful to you. If there is any error, you are also welcome to point out and learn from each other. This is a previously written article about socket blog-socket broadcast iOS-mobile terminal Socket UDP protocol broadcast mechanism implementation 2. What is Bonjour? What can we do? I believe that friends who have never heard of Bonjour will be unfamiliar with this. Bonjor, as the title says, is a zero-configuration Network Protocol for Apple to quit, bonjour can solve the three problems of IP retrieval, name resolution, and service discovery in the absence of a central server.2.1 IP address acquisitionIn a traditional network environment, the IP address of a device is obtained in two ways. One is static configuration. You can manually specify an IP address for the device, and the other is dynamic configuration, the device obtains a dynamic IP address through the DHCP service of the router. In a network environment with no central server, no central server provides DHCP service, and it is inconvenient for users to manually configure IP addresses. This requires a new method to help devices obtain IP addresses, you want the device to manually specify an available IP address. In the IPV6 environment, the IPV6 protocol itself provides the capability for devices to customize IP addresses. Therefore, the implementation is simple. You can simply use the IPV6 protocol for support. In an IPV4 environment, Bonjour uses the method of randomly specifying an IP address. First, the device randomly specifies an IP address belonging to the Region network segment, and then checks whether the address conflicts locally, if there is a conflict, another new IP address is randomly generated until the available IP address is found.2.2 name resolutionIn traditional network environments, the correspondence between names and IP addresses is resolved through DNS. When a device needs to access a domain name, such as "www.saup.com", the device sends "www.saup.com" to the DNS server, and the server returns the IP address corresponding to the domain name, the device then uses the returned IP address to access the target server. In a network environment without a central server, no DNS server provides the domain name resolution service, and name resolution becomes a serious problem. To address this problem, the industry solution is mDNS, which is called "Multicast DNS" in Chinese and defined in RFC6762. The principle of "multicast DNS" is very simple. When a device needs to resolve a name, such as "abc. local. ", this device broadcasts a message to all devices in the local network through UDP and asks 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 UDP protocol and uses the broadcast message method, local name resolution can be completed without a central server providing DNS resolution service. Bonjour is also based on the mDNS protocol, but Bonjour has extended the mDNS protocol, enhancing the device's ability to respond to the "Multicast DNS" request. Under the Bonjour protocol, the application only needs to register a specific name, and then the work in response to the "Multicast DNS" request can be handled by the underlying layer. In other words, under the Bonjour protocol, the application does not need to listen to and respond to the "Multicast DNS" request on the local network. These tasks are completed by the underlying system. To distinguish between a global domain name and a local domain name, The mDNS protocol uses ". local." As the root domain name of the local domain name.2.3 service discoveryWhen a device that provides services obtains an IP address and specifies a domain name, it still cannot meet user requirements. Because users need services such as print services and web Services, users do not care about the server names and IP addresses corresponding to these services. To make it easier for users to discover various services on the local network, Bonjour provides service discovery capabilities for devices. The "service discovery" capability provided by Bonjour is based on a simple and direct provision, that is, the device that provides the service registers the service according to the following standards: "name. service type. transfer protocol type. local. ", for example," DamonWebServer. _ http. _ tcp. local. ", for example," DummiesWebServer. _ http. _ tcp. local. ". In this way, when a device wants to search for the http service, Bonjour searches for the service with "_ http" registered in the local network and returns the result to the user. At this time, the user is facing "DamonWebServer" and "DummiesWebServer". You can ignore what the IP addresses of the two web services are on that device. 3. How to Use Bonjour?Bonjour is mainly used in two parts:3.1.Bonjour Server Registration Service
// First, we need to use the NSNetService class @ property (strong, nonatomic) NSNetService * netService In the iOS SDK;
// Initialize the service and specify the service domain, type, name, and port _ netService = [[NSNetService alloc] initWithDomain: @ "local. "type: @" _ http. _ tcp. "name: @" DamonWebServer "port: 5222];
// Specify the proxy [_ netServicesetDelegate: self]; // publish the registration service [_ netService publish];
// Use @ protocolNSNetServiceBrowserDelegate <NSObject> @ optional/* Sent to the NSNetServiceBrowser instance's delegate before the instance begins a search. the delegate will not receive this message if the instance is unable to begin a search. instead, the delegate will 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 an error in searching for domains or services has occurred. the error dictionary will contain two key/value pairs representing the error domain and code (see the NSNetServicesError enumeration above for error code constants ). it is possible for an error to occur after a search has been started successfully. */-(void) netServiceBrowser :( NSNetServiceBrowser *) aNetServiceBrowser didNotSearch :( NSDictionary *) errorDict;/* Sent to the NSNetServiceBrowser instance's delegate for each domain discovered. if there are more domains, moreComing will be YES. if for some reason handling discovered domains requires significant processing, accumulating domains until moreComing is NO and then doing the processing in bulk fashion may be desirable. */-(void) netServiceBrowser :( NSNetServiceBrowser *) aNetServiceBrowser implements :( NSString *) domainString moreComing :( BOOL) moreComing;/* Sent to the NSNetServiceBrowser instance's delegate for each service discovered. if there are more services, moreComing will be YES. if for some reason handling discovered services requires significant processing, accumulating services until moreComing is NO and then doing the processing in bulk fashion may be desirable. */-(void) netServiceBrowser :( NSNetServiceBrowser *) aNetServiceBrowser didFindService :( NSNetService *) aNetService moreComing :( BOOL) moreComing; /* Sent to the NSNetServiceBrowser instance's delegate when a previusly discovered domain is no longer available. */-(void) netServiceBrowser :( NSNetServiceBrowser *) aNetServiceBrowser didRemoveDomain :( NSString *) domainString moreComing :( BOOL) moreComing; /* Sent to the NSNetServiceBrowser instance's delegate when a previusly discovered service is no longer published. */-(void) netServiceBrowser :( NSNetServiceBrowser *) aNetServiceBrowser didRemoveService :( NSNetService *) aNetService moreComing :( BOOL) moreComing;
3.2.
Bonjour client discovers Local Service
// The client mainly uses NSNetServiceBrowser @ property (strong, nonatomic) NSNetServiceBrowser * serverBrowser In the iOS SDK; @ property (strong, nonatomic) NSMutableArray * servers; // NSNetService is used on the client to parse @ property (strong, nonatomic) NSNetService * netserver; // initialize NSNetServiceBrowser_serverBrowser = [[NSNetServiceBrowseralloc] init]; // specify the proxy _ serverBrowser. delegate = self; _ servers = [NSMutableArrayarray]; // you can search for a service by using the searchForServicesOfType method of the NSNetServiceBrowser instance, you can specify the service type to be searched and the domain [_ serverBrowsersearchForServicesOfType: @ "_ http. _ tcp. "inDomain: @" local. "];
NSNetServiceBrowserDelegate proxy protocol-related proxy Methods
Note: The client can use NSNetService to parse the service. After successful resolution, the client can obtain the details of the communication data, such as IP addresses and ports.
-// The following are several common proxy methods:
// To resolve the service,-(void) netServiceWillResolve :( NSNetService *) netService {NSLog (@ "netServiceWillResolve");} // Resolution Service success-(void) netServiceDidResolveAddress :( NSNetService *) netService {NSLog (@ "service ip: % @, ------ port: % d", netService. addresses, netService. port);} // resolution service failure, resolution error-(void) netService :( NSNetService *) netService didNotResolve :( NSDictionary *) errorDict {NSLog (@ "didNotResolve: % @", errorDict);} // detected service-(void) netServiceBrowser :( NSNetServiceBrowser *) netServiceBrowser didFindService :( NSNetService *) netService moreComing :( BOOL) destroy {NSLog (@ "didFindService "); _ netserver = netService; _ netserver. delegate = self; // set the resolution timeout value [_ netserverresolveWithTimeout: 5.0];}
Author: Clear Saup
Source: http://www.cnblogs.com/qingche/
The copyright of this article is shared by the author and the blog. You are welcome to repost it, but you must keep this statement and provide a connection to the original article on the article page.