6

I've been able to get the samples that come with JmDNS to compile and run, however I can't get any of the classes to discover my services.

I'm running a Windows environment with multiple PC's running VNC, SSH & Apache and I've been trying to get JmDNS to discover at least one of these...

What I ideally want is to be able to detect all running VNC servers on my network. Is there some sort of client and server pairing where I can only discover a service if I've registered it using JmDNS?

Any help getting some results out of the samples will be appreciated, the documentation isn't much help.

import java.io.IOException;
import java.util.logging.ConsoleHandler;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.jmdns.JmDNS;
import javax.jmdns.ServiceEvent;
import javax.jmdns.ServiceListener;

/**
 * Sample Code for Service Discovery using JmDNS and a ServiceListener.
 * <p>
 * Run the main method of this class. It listens for HTTP services and lists all changes on System.out.
 *
 * @author Werner Randelshofer
 */
public class DiscoverServices {

    static class SampleListener implements ServiceListener {
        @Override
        public void serviceAdded(ServiceEvent event) {
            System.out.println("Service added   : " + event.getName() + "." + event.getType());
        }

        @Override
        public void serviceRemoved(ServiceEvent event) {
            System.out.println("Service removed : " + event.getName() + "." + event.getType());
        }

        @Override
        public void serviceResolved(ServiceEvent event) {
            System.out.println("Service resolved: " + event.getInfo());
        }
    }

    /**
     * @param args
     *            the command line arguments
     */
    public static void main(String[] args) {
        try {

            // Activate these lines to see log messages of JmDNS
            boolean log = false;
            if (log) {
                Logger logger = Logger.getLogger(JmDNS.class.getName());
                ConsoleHandler handler = new ConsoleHandler();
                logger.addHandler(handler);
                logger.setLevel(Level.FINER);
                handler.setLevel(Level.FINER);
            }

            final JmDNS jmdns = JmDNS.create();
            String type = "_http._tcp.local.";
            if(args.length > 0) {
                type = args[0];
            }
            jmdns.addServiceListener(type, new SampleListener());

            System.out.println("Press q and Enter, to quit");
            int b;
            while ((b = System.in.read()) != -1 && (char) b != 'q') {
                /* Stub */
            }
            jmdns.close();
            System.out.println("Done");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
Program-Me-Rev
  • 6,184
  • 18
  • 58
  • 142
Ali
  • 12,354
  • 9
  • 54
  • 83

1 Answers1

7

To discover a specific type of service, you need to know the correct service type name, check out DNS SRV (RFC 2782) Service Types:

String bonjourServiceType = "_http._tcp.local.";
bonjourService = JmDNS.create();
bonjourService.addServiceListener(bonjourServiceType, bonjourServiceListener);
ServiceInfo[] serviceInfos = bonjourService.list(bonjourServiceType);
for (ServiceInfo info : serviceInfos) {
  System.out.println("## resolve service " + info.getName()  + " : " + info.getURL());
}
bonjourService.close();

For VNC, use _rfb._tcp.local.
For SSH, use _ssh._tcp.local.
For Apache, use _http._tcp.local.

yorkw
  • 40,926
  • 10
  • 117
  • 130
  • Thanks but my code seems correct, I can vnc into the mac I setup, but I still can't get the server to list even with the code you provided (which is similar to my own code). I've edited the post above to add my testing code. Can you try this on your network and verify if it works? Perhaps something is wrong in my setup somewhere. – Ali Feb 15 '12 at 11:08
  • 1
    If it is a mac machine, you can bonjour the VNC service (Apple Remote Desktop) by using service type = "_net-assistant._udp." – yorkw Feb 15 '12 at 22:56
  • I've had some luck getting the code to detect services on my local machine but not services on the local network. When i execute the jmdns jar, a dialog comes up that has no problems detecting machine on my network. So currently the plan is to step through the source for that dialog and hopefully figure out whats going on. – Ali Feb 16 '12 at 13:21
  • i've found more easy to write my own service discovery implementation: https://github.com/4ntoine/ServiceDiscovery-java. Tested with desktop, android and ios and it has Objective-C implementation too (for both service and client side) – 4ntoine Dec 02 '15 at 10:25
  • I had trouble with this answer too, and the bulk of this here worked in basic java: http://tskotti.blogspot.ca/p/android-device-discovery-with-jmdns.html , no need for the multicast permission stuff. – sjakubowski May 24 '16 at 18:07
  • on my side, serviceInfos size is always zero. I have this JmDNS inside a library (jar) and the code I am calling from Android. Any idea? – Octavian Ionel May 06 '22 at 09:04