与 Bonjour 沟通的最佳方式是什么?

发布于 2024-08-26 01:17:25 字数 1582 浏览 3 评论 0原文

Bonjour 提供了一个很好的方法:DNSSD.browse("_killerapp._tcp", this);。作为该方法的第一个参数,我给出了网络中可能可用的服务类型,作为第二个参数,我给出了“回调对象”。所考虑的方法“浏览”指定类型的服务(第一个参数)。

在浏览过程中,它可以“找到”然后“丢失”服务。如果服务找到(丢失),bonjour 调用回调对象的 serviceFound(serviceLost) 方法。使用找到的服务的一些参数调用 serviceFound。更详细地说:

serviceFound(DNSSDService browser, int flags, int ifIndex, String serviceName, String regType, String domain)

但是要获取服务的IP地址和端口,我们需要执行额外的操作(人们称之为“解析服务”)。这个逻辑对我来说有点奇怪。为什么serviceFound无法提供此信息?我的意思是为什么 Bonjour 在找到服务时无法自动解析服务。

无论如何,我只是接受给定的逻辑并尝试使用它。从 serviceFound 中,我调用 DNSSD.resolve(0, ifIndex, serviceName, regType, domain, this)

和之前一样,我向 resolve 提供了一个回调对象(最后一个参数)。不幸的是,我需要使用不同的类来提供browseresolve的回调对象。原因是 browseresolve 可以调用回调对象的 operationFailed 方法,并且如果我使用来自同一类的回调对象我不知道哪个方法正在调用 operationFailedbrowseresolve)。

因此,我创建了一个单独的类来实例化 resolve 方法的回调对象。在这个类中,我有一个名为 serviceResolved 的方法,由 Bonjour 使用已解析服务的 IP 地址和端口进行调用:

serviceResolved(DNSSDService resolver, int flags, int ifIndex, String fullname, String hostname, int port, TXTRecord txtRecord)

我认为 IP 地址和端口应该是执行浏览的对象的字段。因此,在 serviceResolved 中,我有 IP 和端口,我想将这些值设置为浏览服务的实例的相应字段。但我该怎么办呢?该实例不作为 serviceResolved 方法的参数给出。所以,它是看不见的!

此外,我看到 serviceResolved 和 serviceFound 将 DNSSDService 解析器作为第一个参数。有人知道它是什么吗?这个对象可以用来设置服务的参数吗?我知道这种类型的对象是由 browse 返回的。

There is a nice method provided by Bonjour: DNSSD.browse("_killerapp._tcp", this);. As the first argument of this method I give type of service which potentially can be available in the network, and as the second argument I give a "callback object". The considered method "browse" for the services of the indicated type (first argument).

During the browsing it can "find" and then "lose" a service. If service is found (lost) bonjour call serviceFound (serviceLost) method of the callback object. The serviceFound is called with some parameters of the found service. In more details:

serviceFound(DNSSDService browser, int flags, int ifIndex, String serviceName, String regType, String domain)

But to get the IP address and port of the service we need to do additional operation (people call it "to resolve a service"). This is logic is kind of strange to me. Why this information cannot be given by serviceFound? I mean why Bonjour cannot resolve the service automatically whenever it finds a service.

Anyway, I just accept the given logic and try to use it. From the serviceFound I call DNSSD.resolve(0, ifIndex, serviceName, regType, domain, this).

As before I give a callback object to the resolve (the last argument). Unfortunately I need to use different classes to provide the callback objects for browse and resolve. The reason for that is that browse and resolve can call a operationFailed method of the callback object and, if I use callback objects from the same class I will not know which method is calling the operationFailed (browse or resolve).

So, I create a separate class to instantiate a callback object for the resolve method. In this class I have a method called serviceResolved which is called by Bonjour with IP address and port of the resolved service:

serviceResolved(DNSSDService resolver, int flags, int ifIndex, String fullname, String hostname, int port, TXTRecord txtRecord)

I think that the IP address and port should be fields of the objects which perform browsing. So, in the serviceResolved I have IP and port and I want to set these values to the corresponding field of the instance which browse the service. But how can I do it? This instance is not given as an argument of the serviceResolved method. So, it is invisible!

Moreover, I see that serviceResolved and serviceFound take, as a first argument, DNSSDService resolver. Does anybody know what is it? May be this object can be used to set parameters of the service? I know that an object of this type is returned by the browse.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

揪着可爱 2024-09-02 01:17:25

不是一个真正的答案,但想指出的是,
除了 Bonjour 库之外,您可能还想尝试 JmDNS,它是一个纯 Java 的开源模块。

Not really an answer, but would like to point out that,
besides the Bonjour library, you may want to try JmDNS, which is a pure Java, open sourced module.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文