How to resolve a Bonjour domain name with JmDNS
Asked Answered
C

2

8

As part of an app I'm developing, I need to be able to resolve the correct IP that corresponds with a Bonjour hostname.

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

I've been combing through the JmDNS APIs and the most I can find are methods that allow resolving a Service if the type and name are known. However, I simply cannot find anything that would allow resolving a hostname.

So am I missing something? Is there really no way to resolve a hostname using JmDNS?

Combined answered 17/1, 2013 at 5:35 Comment(4)
I know that its possible, I have done it last year, but im not able to get the right answer for you, I'm not home and am not able to tell you how to do it, i'll yome back when i'm home.Pinchhit
@Pinchhit Awesome! Looking forward.Combined
@Combined Did Raph's solution work for you ? Have you managed to access your android device using a set hostname and not it's IP ?Smaltite
Did you find a solution?Seriocomic
D
0

If you need to find out remote hostname in LAN from IP address using JmDNS you can use the following code. If you need to map from hostname to IP then you can resolve hostnames for all your subnet IPs to build a cache. If your native name resolution supports local Bonjour names you can just 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();
Diao answered 18/1, 2015 at 19:58 Comment(0)
P
-1

With jmdns you listen to services. To subscribe use the functions

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

Once jmdns finds a service the ServiceListener gets notified. The listener has three public functions:

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

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

If you want to resolve the service you have to call

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

in the serviceAdded method.

Have a look at :Quick Tutorial

Pinchhit answered 24/1, 2013 at 13:2 Comment(15)
This assumes that you know the type of service. Once again, as I note in the question, I'm looking for a way to find the IP given the host. So it needs to search by host name...Combined
Have you tried to use the hostname as Service type in the method addServiceListener(String type, ServiceListener listener);Pinchhit
ServiceResolved is not called ever!! Where could be the problem?Frail
'serviceResolved' is only called when you start resolving a service. First only the 'serviceAdded' method is invoked. You have to resolve the service manually.Pinchhit
Though calling jmdns.requestServiceInfo(event.getType(), event.getName()); in serviceAdded, i never get the service resolved,, what could be the problem? (By wireshark, i am sure it sends resolve request)Frail
This is not answering the question, of how to resolve foo.local to its IP address.Brickey
@ElazarLeibovich Yes it is, why should it not be?Pinchhit
@Pinchhit can you please explain then, given a host name "foo.local", which method should I call to convert it to its IP?Brickey
@ElazarLeibovich foo.local has to be resolved using jmDNS for example. It is roughly explained in the answer. For more complete jmDNS example see #8174734 Note: if jack.local is a normal hostname then java.net.InetAddress can be usedPinchhit
@ElazarLeibovich for more information about jmDNS see github.com/jmdns/jmdns For more information about mDNS see en.wikipedia.org/wiki/Zero-configuration_networkingPinchhit
@Pinchhit I understand mDNS and zeroconf, I understand that I need to send a UDP packet to multicast IP and wait for answer. I do not understand how to do that with jmDNS, and this answer didn't help me to understand that.Brickey
@ElazarLeibovich jmDNS does the multicast and stuff for you. You have just to define wich service you are looking for (jmdns.addServiceListener(String type, ServiceListener listener);). Once jmDNS foud one it notifies you by calling the listener you gave it. Does this help?Pinchhit
@Pinchhit no, because I'm not looking for a service (a TXT record in mDNS IIRC). I just want to resolve an address, say String addr = "foo.local". This address has an IP number. How do I get it?Brickey
@ElazarLeibovich If foo.local is just a DNS name you do not need jmDNS. If foo.local is a mDNS servise you can do it like i described in the answer. If foo.local is something else, i do not know how to do it, sorry.Pinchhit
@Pinchhit foo.local is an mDNS name. It is the hostname of a machine, and is resolved like a DNS server but on a specific multicast port. I think that this was the question.Brickey

© 2022 - 2024 — McMap. All rights reserved.