2012-04-25 87 views
1

嗨我创建了示例jmdns serviceListner,当我运行它时,它应该继续列出服务,因为它发现,但与此我的程序,它列出几个服务运行时,之后它没有列出任何服务。我在我的谷歌Chrome浏览器中有dns-sd,所以当我在Chrome中浏览服务时,当时我的程序列出了其他服务,否则我的程序不会列出服务。我不明白为什么ServiceListner本身不检测任何后续事件。JMdns ServiceListner没有找到所有服务,

public static void main(String[] args) throws Exception { 
    String type = "_my-app._tcp.local."; 
    Enumeration<NetworkInterface> ifc = NetworkInterface.getNetworkInterfaces(); 
    while (ifc.hasMoreElements()) { 
     NetworkInterface anInterface = ifc.nextElement(); 
     if (anInterface.isUp()) { 
      Enumeration<InetAddress> addr = anInterface.getInetAddresses(); 
      while (addr.hasMoreElements()) { 
       InetAddress address = addr.nextElement(); 
       final JmDNS jmdns = JmDNS.create(address, type); 
       ServiceListenerClass aClass = new ServiceListenerClass(); 
       jmdns.addServiceListener(type, aClass);    

      } 
     } 
    } 


} 

public static class ServiceListenerClass implements ServiceListener { 
    public void serviceAdded(ServiceEvent event) { 
     event.getDNS().requestServiceInfo(event.getType(), event.getName(), true); 
    } 

    public void serviceRemoved(ServiceEvent event) { 
     System.out.println((count--) + " " + event.getInfo().getName()); 
    } 

    public void serviceResolved(ServiceEvent event) { 
      System.out.println((count++) + " :Res: " + event.getInfo().getName() + " " + event.getInfo().getPort() + " " + event.getInfo().getApplication() 
        + " " + event.getInfo().getDomain() + " " + event.getInfo().getKey()); 
    } 
} 
+0

为什么你绑定到每个单独的接口? – 2012-04-25 19:00:59

+0

因为我在PC上运行它所以,以确保它可以接受形式的所有接口,也尝试没有绑定到接口但结果是一样的,所以我没有得到什么问题,而令人惊讶的是,当我从铬浏览dns-sd运行上面的代码开始列出剩余服务。 – twid 2012-04-26 09:27:18

+0

0.0.0.0接口应该监听所有接口。 – 2012-04-26 10:31:08

回答

1

我有类似的问题两个。 该问题可能来自您的防火墙设置。

在我的情况下,javaw.exe可以访问所有传入的呼叫,但防火墙阻止它时无法向任何人发送任何信息。

尝试转动防火墙来测试你的程序,并确保不是防火墙引起的问题。

+0

谢谢我没有试过,但没有工作,正如我在我最后的评论中提到的,我没有更新到新的快照,然后很好地工作..我注意到JMDNS正在从服务器接收多路邮件,但无法提供服务听众类.. – twid 2012-07-26 11:14:21

+0

你为什么后增量你计数变量,而不是预增量? (将其更改为--count和++计数以获得正确数量的检测服务) – RaphMclee 2012-07-31 05:57:39

+0

是的您是对的......但我的问题是JmDNS Library无法将所有服务事件传递给侦听器类(ServiceListenerClass)。因此,除非ServiceListenerClass无法获得任何事件通知,否则无法显示服务信息。谢谢... – twid 2012-08-01 10:59:56

0

同样的问题在这里。用以下参数启动java解决了它对我来说:

-Djava.net.preferIPv4Stack=true