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:
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:
The above will use the default mDNS implementation (JmDNS). Alternatively you can use the following to use JmDNS explicitly:
Or mDNSResponder instead:
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,>();
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());