使用JmDNS的样本 [英] Samples with JmDNS

查看:191
本文介绍了使用JmDNS的样本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经能够获得JmDNS附带的样本进行编译和运行,但是我无法获得任何类来发现我的服务。

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.

我正在运行Windows环境,其中有多台PC正在运行VNC,SSH和Linux。 Apache和我一直试图让JmDNS发现其中至少一个......

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...

我理想的是能够检测所有正在运行的VNC服务器我的网络。是否存在某种客户端和服务器配对,如果我使用JmDNS注册它,我只能发现服务?

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


推荐答案

要发现特定类型的服务,您需要知道正确的服务类型名称,请查看 DNS SRV(RFC 2782)服务类型

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

对于VNC,请使用_rfb._tcp.local。

对于SSH,请使用_ssh._tcp.local。

对于Apache,请使用_http._tcp.local。

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

这篇关于使用JmDNS的样本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆