1

我正在尝试使用这篇博客文章作为参考来学习如何使用 Bonjour:

http://marknelson.us/2011/10/25/dns-service-discovery-on-windows/

我已经下载了链接在该页面底部的示例项目,它就像魅力一样。现在我试图在我的控制台应用程序中从头开始重现服务发现:

#include <iostream>
#include <assert.h>   
#include "dns/dns_sd.h"

class CDnsSd 
{
public:
    bool discoverAsync ();

private:
    static void DNSSD_API onDiscoveryFinished (DNSServiceRef sdRef, DNSServiceFlags flags, uint32_t interfaceIndex, 
                              DNSServiceErrorType errorCode, const char *serviceName, const char *regtype,
                              const char *replyDomain, void *context);
};

bool CDnsSd::discoverAsync()
{
    DNSServiceRef client = NULL;
    const DNSServiceErrorType err = DNSServiceBrowse( &client, 0, 0, ""_services._dns-sd._udp"", "", onDiscoveryFinished, this );
    return err == kDNSServiceErr_NoError;
}

void DNSSD_API CDnsSd::onDiscoveryFinished( DNSServiceRef sdRef, DNSServiceFlags flags, uint32_t interfaceIndex, DNSServiceErrorType errorCode, const char *serviceName, const char *regtype, const char *replyDomain, void *context )
{
    std::cout << __FUNCTION__;
}

void main ()
{
    CDnsSd dnsSd;
    const bool ret = dnsSd.discoverAsync();
    assert(ret);
    Sleep(10000000);
}

DNSServiceBrowse返回kDNSServiceErr_NoError,但从不调用回调。怎么了?

4

2 回答 2

1

您需要一个处理 Bonjour 事件的主循环。仔细查看您提供的链接。它位于“驱动回调”部分。

于 2013-08-26T21:27:22.493 回答
0

我必须调用名为的方法以DNSServiceProcessResult使其对我有用。我希望它有帮助

于 2018-06-13T08:39:49.183 回答