06:13:07 pm on April 18, 2010 |
Multicast DNS (MDNS) is a little-known feature of DNS that allows you to automatically discover services and appliances attached to your network. Chances are you’re already using it with various household protocols such as Universal Plug and Play (UPNP) and Zeroconf/Bonjour.
Basically, services and appliances can discover each other just by plugging them into the local network: nice and simple!
Obviously, this makes a natural fit for smart phone applications, where simplicity is a virtue so it would be nice if my Android apps could use MDNS. Fortunately, a sophisticated MDNS Java API already exists: JmDNS. JmDNS is a nice fit for Android because it has no external dependencies and it’s already threaded.
Unfortunately, JmDNS is also poorly documented. Recently, I’ve been trying to get JmDNS to play nice in Android, so I thought I’d make a note of a couple of the gotchas I’ve come across:
- Catch Service Resolved Events: There are three stages for service discovery within JmDNS: services are added, resolved and then removed. Events exist for each of these. A service added event indicates that a service has been found only: if you want to connect to the service you don’t want to catch this event. Instead, you want the service resolved event. This indicates that all relevant service information, including the IP address and port number have been found.
- Make sure your services are running on IPv4: Multicast DNS resolves the service’s host IP address by decoding it from the DNS messages sent from an MDNS server. If your service is running IPv6 this might cause problems: the Android emulator does not support IPv6, so if it decodes an IPv6 address you’ll get a “bad socket” exception when you attempt to open a connection. You might need to disable IPv6 on your server operating system.
That said, there is some evidence that Android doesn’t actually support Multicast DNS on handsets, so we’ll see how far we get but, hopefully, if you’re having problems this will at least get you started.Advertisements