我正在尝试使用此博客文章作为参考来学习如何使用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
,但永远不会调用回调。怎么了?
答案 0 :(得分:1)
你需要一个主循环处理Bonjour事件。仔细查看您提供的链接。它出现在“驾驶回调”部分。
答案 1 :(得分:0)
我不得不调用名为DNSServiceProcessResult
的方法来使其适用于我。
我希望它有所帮助