Duplicate Bonjour services while browsing

Q: While browsing for Bonjour services using CFNetSevices, NSNetServices or DNSServiceDiscovery, I sometimes get multiple ADD events containing identical service names. Why does that happen?

A: This happens when your computer has more than one network interface enabled because, by default, Bonjour browses on all active interfaces. So for example, if your computer has an Ethernet connection and an AirPort connection, and a service is being advertised on both Ethernet and AirPort, your callback will be called twice containing the same service name.

With CFNetServices and NSNetSerivces, there's no way to know which network interface the service was discovered on, so it's best to assume that both service names represent the same service. You probably don't want to show the same name multiple times in a list of services, so you should do reference counting of each service name. Every time you get an ADD event for a service name you should increment the reference count, and for each REMOVE event, you should decrement the reference count. When the reference count reaches zero, you should remove the service name from your browser list. When it comes time to Resolve a service that was discovered on multiple network interfaces, it makes no difference which CFNetServiceRef or NSNetService object you use to initiate the Resolve, because the Resolve query gets sent on every network interface.

With the newer DNSServiceDiscovery API, located in /usr/include/dns_sd.h, your browse callback will return a network interface ID that corresponds to the interface on which the service was discovered. This gives you the option of displaying the service name multiple times along with extra information describing the network interface. For example, you could display a service name with the text "en1" or "AirPort" appended to the name, or you could simply display an AirPort icon next to the service name if the service was discovered over AirPort. However, if you're using the DNSServiceDiscovery API and thus can distinguish services on different interfaces, it may still make sense to show each service name only once (using the reference counting approach described above) because this makes the user interface simpler.

Document Revision History

DateNotes
2004-02-06Explains how to work with multiple network interfaces when browsing for Bonjour services.

Posted: 2004-02-06


Did this document help you?
Yes: Tell us what works for you.
It’s good, but: Report typos, inaccuracies, and so forth.
It wasn’t helpful: Tell us what would have helped.