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?

George Profenza
  • 50,687
  • 19
  • 144
  • 218
Jake
  • 283
  • 2
  • 6

2 Answers2

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();
-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

RaphMclee
  • 1,623
  • 13
  • 16
  • 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... – Jake Jan 25 '13 at 07:11
  • Have you tried to use the hostname as Service type in the method addServiceListener(String type, ServiceListener listener); – RaphMclee Aug 08 '13 at 13:50
  • `ServiceResolved` is not called ever!! Where could be the problem? – nmxprime Mar 03 '14 at 11:17
  • 'serviceResolved' is only called when you start resolving a service. First only the 'serviceAdded' method is invoked. You have to resolve the service manually. – RaphMclee Mar 03 '14 at 15:33
  • 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) – nmxprime Sep 23 '14 at 10:32
  • This is not answering the question, of how to resolve `foo.local` to its IP address. – Elazar Leibovich Nov 28 '16 at 12:17
  • @ElazarLeibovich Yes it is, why should it not be? – RaphMclee Jan 10 '17 at 16:15
  • @RaphMclee can you please explain then, given a host name "foo.local", which method should I call to convert it to its IP? – Elazar Leibovich Jan 10 '17 at 18:54
  • @ElazarLeibovich foo.local has to be resolved using jmDNS for example. It is roughly explained in the answer. For more complete jmDNS example see http://stackoverflow.com/questions/8174234/jmdns-service-discovery-in-client-server Note: if jack.local is a normal hostname then java.net.InetAddress can be used – RaphMclee Jan 11 '17 at 10:08
  • @ElazarLeibovich for more information about jmDNS see https://github.com/jmdns/jmdns For more information about mDNS see https://en.wikipedia.org/wiki/Zero-configuration_networking – RaphMclee Jan 11 '17 at 10:21
  • @RaphMclee 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. – Elazar Leibovich Jan 11 '17 at 18:10
  • @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? – RaphMclee Jan 15 '17 at 18:54
  • @RaphMclee 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? – Elazar Leibovich Jan 16 '17 at 06:06
  • @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. – RaphMclee Jan 16 '17 at 06:43
  • @RaphMclee 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. – Elazar Leibovich Jan 16 '17 at 11:01