Chapter 9. Using the CFNetwork and Cocoa APIs


In the previous three chapters, you have learned about techniques and APIs that work on multiple platforms. In Chapter 6, we covered the general concepts and the dns-sd command-line tool, which apply to all the supported platforms. In Chapter 7 we covered the DNSServiceDiscovery C API, which is available on all the supported platforms. In Chapter 8, you saw the Java API, which lets you write a Java program that will run on any supported platform with Java installed.

If you're writing a program in C that's built around a Core Foundation CFRunLoop as its central event-handling mechanism, or a program in Objective-C that's built around a Cocoa RunLoop as its central event-handling mechanism, you have two options. The first option is that you can use the standard C DNSServiceDiscovery API and add those active operations as event sources to your RunLoop, as shown in the Core Foundation and Cocoa examples toward the end of Chapter 7. If you're comfortable mixing standard C in with your Core Foundation-style or Cocoa-style programming, this is fine. However, if you prefer to stick to a single programming style, then you have a second option: Apple has provided additional wrapper APIs that follow the Core Foundation and Cocoa idioms.

In this chapter, you will look at how to perform the basic DNS-SD operations of registering a service, browsing for services, and resolving a service using these Mac OS X-specific APIs, which Apple has provided for the benefit of Core Foundation and Cocoa programmers. As always, the key to creating a good Zeroconf application is in understanding and correctly implementing asynchronous calls. When programming using the CFNetServices API, you pass in a reference to the callback function, much as you did in Chapter 7 with the plain C API. When programming using the Cocoa NSNetServices API, you specify a delegate object that will be notified of interesting events, much as you did in Chapter 8 with the Java API. Just as in the Java API, the delegate or event listener object is usually self. Although the exact mechanisms are different, you use them in analogous ways. If you have not already done so, you will find it helpful to read the section "Asynchronous Programming Model" in Chapter 7 before working with either the CFNetServices or the Cocoa NSNetServices APIs.




Zero Configuration Networking. The Definitive Guide
Zero Configuration Networking: The Definitive Guide
ISBN: 0596101007
EAN: 2147483647
Year: 2004
Pages: 97

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net