How to resolve a Bonjour domain name using JmDNS

As part of the application I'm developing, I need to be able to resolve the correct IP address that matches the Bonjour hostname .

For example, I'm assigned jack.local and need to resolve it to 192.168.1.141 , which is the IP address associated with the socket.

I combed the JmDNS APIs, and most of them that I can find are methods that allow the Service to be resolved if the type and name are known. However, I just can’t find anything that would resolve the host name.

Am I missing something? Is it not possible to resolve the host name using JmDNS?

+8
java android hostname bonjour jmdns
source share
2 answers

If you need to find the remote host name on the local network from an IP address using JmDNS, you can use the following code. If you need to match hostname and IP, you can resolve hostnames for all of your subnet IP addresses to create a cache. If your own name resolution supports local Bonjour names, you can simply use InetAddress.getByName (hostname) .getHostAddress ().

  final JmDNSImpl jmdns = new JmDNSImpl(null, null); final HostInfo hostInfo = HostInfo.newHostInfo(InetAddress.getByName("192.168.1.78"), jmdns, null); System.out.println("MDNS hostname (Bonjour): " + hostInfo.getName()); System.out.println("DNS hostname: " + hostInfo.getInetAddress().getHostName()); System.out.println("IP address: " + hostInfo.getInetAddress().getHostAddress()); jmdns.close(); 
0
source share

Using jmdns you are listening to services. To subscribe, use the functions

 jmdns = JmDNS.create(); jmdns.addServiceListener(String type, ServiceListener listener); 

As soon as jmdns finds a service, a notification ServiceListener will be notified. The listener has three public functions:

 serviceResolved(ServiceEvent event) serviceRemoved(ServiceEvent event) serviceAdded(ServiceEvent event) 

with all ServiceEvent recipients. Now call event.getInfo().getHostAddresses() to get an array of all Host addresses.

If you want to allow the service, you must call

 jmdns.requestServiceInfo(event.getType(), event.getName(), 1); 

in the serviceAdded method.

Take a look at: Quick Start Guide

-one
source share

All Articles