简体   繁体   English

JmDNS设备删除检测

[英]JmDNS device removal detection

I am trying to use JmDNS to detect addition and removal of devices on the network. 我正在尝试使用JmDNS来检测网络上设备的添加和删除。 The addition works fine, the devices are found as soon as their respective services are registered with JmDNS, but, when a device is removed from the network, it is never registered with JmDNS. 添加工作正常,只要各自的服务在JmDNS中注册,就会找到设备,但是,当从网络中删除设备时,它永远不会在JmDNS中注册。

I have tried invoking jmDns.list(serviceName) periodically, but, it seems, as if it always returns cached values. 我已经尝试定期调用jmDns.list(serviceName) ,但似乎它总是返回缓存值。

Is it possible to make JmDNS register removed devices? 是否可以使JmDNS寄存器被删除?

Here is the code I used to register service and service listener (only relevant parts): 这是我用来注册服务和服务监听器的代码(只有相关部分):

Service: 服务:

JmDNS jmdns = JmDNS.create();
ServiceInfo service = ServiceInfo.create("_test._tcp.local.", "Test", 1234, 0, 0, false, "Test info");
jmdns.registerService(service);

Service listener: 服务听众:

JmDNS jmDns = JmDNS.create();
jmDns.addServiceListener("_test._tcp.local.", new ServiceListener() {
    @Override
    public void serviceAdded(ServiceEvent event) {
        System.out.println("Service added " + event.getInfo());
    }

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

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

Method serviceRemoved() is never invoked, even though the device previously registered is not powered on anymore. 方法serviceRemoved()永远不会被调用,即使先前注册的设备不再启动。 I am very grateful for any help I can get in solving the issue. 我非常感谢能够解决这个问题的任何帮助。

Update : JmDNS does figure out that the device is missing from the network, but only after half an hour. 更新 :JmDNS确实发现网络中缺少设备,但仅在半小时后。 Is it possible to make this period shorter? 是否有可能缩短这段时间?

The serviceRemoved() method is only called when the removed device unregisters itself before shutting down. 仅当被删除的设备在关闭之前取消注册时,才会调用serviceRemoved()方法。 JmDNS is not actively checking the presence of the devices but just listening to the broadcast messages. JmDNS没有主动检查设备的存在,只是收听广播消息。 If the removed device has not unregistered itself JmDNS does never know it util the timeout hits in (probably 30minutes in your case). 如果删除的设备本身没有注册,JmDNS永远不会知道它的超时命中率(在你的情况下可能是30分钟)。 One possibility to check if a device is still there is to actively ask it. 检查设备是否仍然存在的一种可能性是主动询问它。 There are several ways to do this. 有几种方法可以做到这一点。 I dont know if JmDNS has already something build if for that. 我不知道JmDNS是否已经构建了一些内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM