客户端-服务器中的 JmDNS 服务发现

JmDNS service discovery in client-server(客户端-服务器中的 JmDNS 服务发现)

本文介绍了客户端-服务器中的 JmDNS 服务发现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 JmDNS 在我的客户端-服务器应用程序中启用服务发现.我完全理解服务器端的服务注册表,代码如下:

I'm trying to enable service discovery in my client-server application using JmDNS. I fully understand service registry on the server side, with code that resembles this:

JmDNS jmdns = JmDNS.create(localhost);
jmdns.register(serviceInfo);

但是,我无法弄清楚如何让我的客户端从注册的服务中检索端口号和 IP 地址,并使用这些数据打开 TCP 连接.我搜索了如何使用 JmDNS 的示例,但无济于事.这里有人可以给我一些基本的例子吗?或者,如果有人有任何 JmDNS 上好的资源/教程的链接,他们可以提供吗?

However, I'm having trouble figuring out how to have my client retrieve the port number and IP address from the registered service and use this data to open a TCP connection. I've searched for examples of how to use JmDNS but to no avail. Can anyone here give me some basic examples? Or if anyone has any links to good resources/tutorials on JmDNS could they please provide them?

注意:JmDNS 在这里是必需的,所以我希望得到仅与 JmDNS 相关的答案,而不提供替代方案.另外,我已经广泛研究了 JmDNS API,但仍然无法弄清楚,所以请不要将文档链接作为资源发布.

Note: JmDNS is a neccessity here, so I would appreciate answers pertaining only to JmDNS and not offering an alternative. Also, I have looked at the JmDNS API extensively, and still can't figure it out, so please don't post a link to the docs as a resource.

谢谢.

推荐答案

服务器端:

mdnsServer = JmDNS.create(localhost);
// Register a test service.
ServiceInfo testService = ServiceInfo.create("my-service-type", "Test Service", 6666, "test service");
mdnsServer.registerService(testService);

客户端:

private ServiceListener mdnsServiceListener = new ServiceListener() {
  public void serviceAdded(ServiceEvent serviceEvent) {
    // Test service is discovered. requestServiceInfo() will trigger serviceResolved() callback.
    mdnsService.requestServiceInfo(Constants.mdnsServiceType, serviceEvent.getName());
  }

  public void serviceRemoved(ServiceEvent serviceEvent) {
    // Test service is disappeared.
  }

  public void serviceResolved(ServiceEvent serviceEvent) {
    // Test service info is resolved.
    String serviceUrl = serviceEvent.getInfo().getURL();
    // serviceURL is usually something like http://192.168.11.2:6666/my-service-name
};

mdnsService = JmDNS.create();
mdnsService.addServiceListener("my-service-type", mdnsServiceListener);
ServiceInfo[] infos = mdnsService.list(Constants.mdnsServiceType);

// Retrieve service info from either ServiceInfo[] returned here or listener callback method above.
mdnsService.removeServiceListener("my-service-type", mdnsServiceListener);
mdnsService.close();

一旦你有了 URL http://192.168.11.2:6666/myTestService,你就可以解析/使用它打开套接字连接.希望有所帮助.

Once you have the URL http://192.168.11.2:6666/myTestService, you can parse/use it open socket connection. Hope that help.

这篇关于客户端-服务器中的 JmDNS 服务发现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:客户端-服务器中的 JmDNS 服务发现

基础教程推荐