How To use the Java mDNS Wrapper

A few days ago I posted an article about my wrapper API which unifies the mDNSResponder and JmDNS packages behind a single interface, but I neglected to provide usage examples or a usage guide. I’ll try and correct that now.

In addition to unifying multiple mDNS implementations under a single API, I think my API reduces the amount of work needed by the developer for registering and listening for services to a minimum.

Building the code
First off, all you should need to compile and run my package is Maven. Once you have Maven installed, cd into the jmdns directory, and run:

mvn test

This will compile JmDNS, my add-on API, and run a few unit tests. The API in question is tested by org.jmdns.api.DiscoveryFactoryTest. I would expect the JmDNS portion of the test testJmDNS() to pass under almost any circumstance, but the testmDNSResponder() will only pass if you are running on Linux and have mDNSResponder (or possibly the avahi compatibility layer) installed successfully.

Example usage – Initialization

Before registering or listening for services, you need to initialize the discovery subsystem:

DiscoveryFactory.initRegistry( DiscoveryFactory.DISCOVERY_REGISTRY_DEFAULT);

The above will use the default mDNS implementation (JmDNS). Alternatively you can use the following to use JmDNS explicitly:

DiscoveryFactory.initRegistry( DiscoveryFactory.DISCOVERY_REGISTRY_JMDNS);

Or mDNSResponder instead:

DiscoveryFactory.initRegistry( DiscoveryFactory.DISCOVERY_REGISTRY_MDNSRESPONDER);

Consider this an initialization a singleton, you should do this once and only once per JVM instance.

Example usage – Registering a Service

Assuming the system is initialized, the following code will register a service:

HashMap<string,> props = new HashMap<string,>(); props.put("type", "website");</string,></string,>

IDiscoveryRegistry registry = DiscoveryFactory.getRegistry();

IServiceInfo myService = registry.registerService("_http._tcp", "mySite", null, 80, props);

Note the ‘null’ in the registerService() call is to use the default domain (.local). You can pass a domain name here to register in another domain.

To unregister your service, just do:


Example usage – Listening for a Service

Create a listener:

BaseDiscoveryListener listener = new BaseDiscoveryListener("_http._tcp", "website", null);

Wait 15 seconds for a service:


Get the first service found:

IServiceInfo service = listener.getFirstService();

Print out some information on the service:

System.out.println("service name: " + service.getName() + " at " + service.getHostAddress() + ", port: " + service.getPort());

About admin

A software architect and engineer with a fine arts background. You can connect with him on Google+.
This entry was posted in Tech and tagged , , , , , , , , . Bookmark the permalink.

7 Responses to How To use the Java mDNS Wrapper

  1. mathieu says:

    just a suggestion, don’t use a HashMap in argument signature, Map is enough.

    Thanks for your work.

  2. pardsbane says:

    Good point, I’ll update the API, thanks.

  3. bjb says:

    An more standard way would be an JNDI implementation 😉

    Doing so, you could simply perform SRV looup on the multicast provider 😀

  4. pardsbane says:

    That’s an interesting idea. I’m not very familiar with JNDI, but one of the reasons for the wrapper is to support using Apple’s native mDNSResponder where available without having to re-implement the whole protocol. Could a JNDI provider be written around mDNSResponder and seperately against JmDNS?

    I was also thinking of an OSGi plugin interface, allowing either JmDNS or mDNSResponder to implement it.

  5. dd says:


    we recently used your wrappers, which I find usefull. I’m looking to port avahi too, which would be preferred on Linux machines. Is there any update of the current library before I start this work ?

  6. pardsbane says:


    I’m sorry, I haven’t ported my wrappers to Avahi, which I agree would be very useful and I’d like to do, but don’t have the development resources right now.

    One option would be to install the Avahi mDNSResponder compatibility package. I think the mDNSResponder Java libraries will run against them. The trouble with this is the installation process would get a bit funny.

    Another alternative would be to use the Java DBUS library and access Avahi that way.

    I’ve got a version of JmDNS 1.1 that has my wrapper embedded in it and a few other patches as well, I can share that with you if you’d like. I haven’t had a chance to sync this up with JmDNS 2.0, which I saw was released recently.

  7. Pingback: Distributed Clipboard – kicked off « DSGPB – DSG Praktika Blog

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>