Mac OS X provides several layers of application programming interface (API) for Bonjour service applications: The NSNetService and NSNetServiceBrowser classes in the Foundation framework; CFNetServices, part of the CFNetwork framework in Core Services; DNS Service Discovery for Java; and the low-level DNS Service Discovery API built around BSD sockets. All three API sets provide facilities for publication, discovery, and resolution of network services. Figure 1 illustrates the structure of the API layers. As you can see, the Multicast DNS responder (or other DNS server) sits at the lowest level, so your software does not have to interact directly with DNS.
NSNetService and NSNetServiceBrowser
CFNetServices
DNS Service Discovery
The NSNetService and NSNetServiceBrowser classes, part of the Foundation framework in Cocoa, provide object-oriented abstractions for service discovery and publication. NSNetService objects represent instances of Bonjour services, either for publication or a service discovered by a client, and NSNetServiceBrowser represents a browser for a particular type of service. Most Cocoa programmers should find these classes sufficient to meet their needs. If you need more detailed control, you can use the DNS Service Discovery API from a Cocoa application.
NSNetService and NSNetServiceBrowser are scheduled on the default NSRunLoop object to perform publication, discovery, and resolution asynchronously. All results returned by NSNetService and NSNetServiceBrowser objects are handled by delegate objects. These objects must be associated with a run loop to function, but it need not be the default one.
The CFNetServices API declared in the Core Services framework provide Core Foundation-style types and functions for managing services and service discovery. CFNetServices defines three Core Foundation object types, CFNetService, CFNetServiceBrowser, and CFNetServiceMonitor. CFNetService is an abstract representation of a service instance, either for publication or for use. Associated functions provide support for publishing and resolving services. CFNetServiceBrowser represents a browser for a particular type of service in a particular domain. You should use this API from C or C++ applications for Mac OS X.
Both CFNetService and CFNetServiceBrowser objects are normally serviced in CFRunLoops. To retrieve results, applications implement callback functions to handle events, such as new services appearing or disappearing, instances being resolved, and errors occurring. Unlike NSNetService and NSNetServiceBrowser, CFNetServices types do not require a run loop and can run synchronously if this behavior is needed. However, it is bad practice to use the synchronous modes of these functions.
The DNS Service Discovery API, declared in </usr/include/dns_sd.h>
, provide low-level BSD socket communication for Bonjour services. DNS Service Discovery acts as an intermediate layer between your software and the Multicast DNS responder or DNS server. It manages the Multicast DNS responder for you, letting you write your program in terms of services and service browsers instead of DNS resource records.
Because the DNS Service Discovery API is part of the Darwin open source project, you should use it when programming for Darwin. In some cases, using this API is simpler than CFNetServices, but DNS Service Discovery does not provide the same integration with the Core Foundation programming model.
DNS Service Discovery is also the API that should be used if developing Bonjour service applications for Windows, Linux, or FreeBSD.
© 2006 Apple Computer, Inc. All Rights Reserved. (Last updated: 2006-05-23)