Bonjour network services

来源:互联网 发布:python视频谁讲的好 编辑:程序博客网 时间:2024/05/16 14:53

转自https://developer.apple.com/library/ios/#documentation/Networking/Conceptual/NSNetServiceProgGuide/Articles/OperationsonNetworkServices.html#//apple_ref/doc/uid/TP40002525-SW1

Foundation Network Services Architecture

This article describes the structure of the Foundation classes used for network services, and how the methods in these classes operate.

Foundation Classes for Network Services

The Foundation framework defines two classes for managing Bonjour network services. These classes correspond to the two basic elements of network services: services and service browsers. NSNetService represents an actual instance of a service, and provides methods both for publishing a local service to the network and for connecting to a remote service. NSNetServiceBrowser acts as a browser for a particular type of service; it queries the network for available services and collects the results.

NSNetService

The NSNetService class represents a single instance of a service. The service can either be a remote service that your application wants to use, or a local service your application is publishing; however, it is never both. NSNetService instances perform all operations asynchronously, returning results to a delegate object for processing.

NSNetService objects that represent remote services are initialized with the service name, type, and domain, but no host name, IP address or port number. The name, type, and domain are used to resolve the instance into socket information (IP address and port number) so your application can connect to the service.

NSNetService objects used for publishing local services to the network are initialized with the service name, type, and domain, and also the service’s port number. They use this information to announce the necessary information to the network through the multicast DNS responder.

NSNetServiceBrowser

An NSNetServiceBrowser object represents one of two things: A browser for a single type of service, or a browser for domains. Like NSNetService, NSNetServiceBrowser instances perform all operations asynchronously, returning results to a delegate object for processing. At any given time, a single NSNetServiceBrowser object can execute at most one search operation; if you need to search for multiple types of services at once, use multiple NSNetServiceBrowser objects.

Most of the time, you use NSNetServiceBrowser to search for a specific type of service. For example, you might set up a service browser to search for FTP services available on the local network and present the list of services to the user, letting the user connect to one of them. The results of such a search are instances of NSNetService corresponding to each remote service.

You can also use NSNetServiceBrowser to search for available domains. By passing NSNetServiceBrowser the empty string (@"") for the domain, it will search for all potential domains. However, just because you have access to all available domains, does not mean that you should begin searching through them sequentially. It is best to present the user with the list of domains, allowing him to select one which can then be scanned for services.

Asynchronous Results and Delegate Objects

Due to the length of time it takes for network discovery, the NSNetService and NSNetServiceBrowser APIs perform all their operations asynchronously. The methods provided by NSNetService and NSNetServiceBrowser return immediately, so your application can continue executing while network operations take place. However, your application still needs to process the information returned by the objects. Instead of requiring you to subclass NSNetService and NSNetServiceBrowser to handle results, both classes send the results of network operations to delegate objects that implements the appropriate methods to handle the results.

A common network operation is resolving a service instance name—such as 3rd Floor Copy Room._printer._tcp.local.—into socket information (IP address and port number). Figure 1 illustrates how this resolution process happens asynchronously and returns results to the delegate object.

At some point (step 1), a message is sent to the NSNetService object requesting that it retrieve socket addresses for the service; this method returns immediately. In step 2, the application continues execution as the resolution proceeds. At some later point in time, the resolution finishes (step 3), and the NSNetService object returns results to its delegate object.

Figure 1  Asynchronous service resolution returning results to a delegate object

Operations on Network Services

The architecture for Bonjour network services in the Foundation framework abstracts your application away from the details of DNS record management, and lets you manage services in terms of the three fundamental operations defined for Bonjour network services:

  • Publication (advertising a service)

  • Service discovery (browsing for available services)

  • Resolution (translating service names to addresses and port numbers for use)

In Cocoa applications, you perform these operations using NSNetService and NSNetServiceBrowser objects. NSNetService objects represent instances of Bonjour net services, and are involved in all three operations. The NSNetServiceBrowser class, as its name suggests, defines an interface for service discovery; it also lets you search for domains available for publication and browsing.

The relationship between these operations and the two classes is discussed in the following sections.

Publication

The NSNetService class handles service publication, as illustrated in Figure 2. To publish a service, an application first sets up a valid socket for communication, for example with NSSocketPort (step 1). Once the socket is ready, the application initializes an NSNetService object with the socket and domain information (step 2), and sets up a delegate object to receive results. The object automatically registers itself with the current run loop. In step 3, the application sends a message to the NSNetService object requesting that the service be published to the network, and publication proceeds asynchronously. In step 4, the NSNetService object returns results to your delegate.

When the service is about to be published, the delegate is notified. If publication fails for any reason, the delegate is also notified, along with appropriate error information. If publication proceeds successfully, no further messages are sent to the delegate.

For step-by-step instructions and code examples about service publication, see “Publishing Network Services.”

Figure 2  Service publication with NSNetServiceService publication with NSNetService

Service Discovery

To discover services advertised on the network, you use the NSNetServiceBrowser class, as shown in Figure 3. In step 1, the application initializes an NSNetServiceBrowser object and associates a delegate object to it. In step 2, the NSNetServiceBrowser object searches asynchronously for services. In step 3, results are returned to the delegate object. The last step can happen a number of times as new services are found on the network. A browser can search for an extended period of time, and the delegate is notified as new services come online or shut down.

Figure 3  Service discovery with NSNetServiceBrowserService discovery with NSNetServiceBrowser

For step-by-step instructions and code examples about service discovery, see “Browsing for Network Services.”

Resolution

If you know the name, type, and domain of a service, you can initialize an NSNetService object with this information, and ask the object to resolve the service name into socket addresses. To prevent your application from slowing down, resolution also takes place asynchronously, returning results or error messages to the NSNetService object’s delegate. If valid addresses were found for the service, your application can then use the addresses to make a socket connection.

Figure 4 illustrates this process. In step 1, the application initializes an NSNetService instance for the service, in this case a local music service over TCP calledEd's Party Mix. In step 2, the NSNetService object receives a resolve message. The resolution proceeds asynchronously, and at some point it receives an IP address and port number for the service (169.254.150.84:1010). In step 3, the delegate is notified, and in step 4, the delegate asks the NSNetService object for a list of addresses and port numbers, in this case just one of each.

For step-by-step instructions and code examples about service resolution, see “Resolving and Using Network Services.”

Figure 4  Service resolution with NSNetServiceService resolution with NSNetService

Browsing for Domains

NSNetServiceBrowser objects can also search for domains in much the same way as they search for services. Instead of returning NSNetService objects to delegate objects, they return domain names.

For step-by-step instructions and code examples about domain browsing, see “Browsing for Domains”

原创粉丝点击