2

I'm trying to learn how to use Bonjour using this blog article as a reference:

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

I've download sample project linked at the bottom of that page, it works like charm. Now I'm trying to reproduce service discovery from scratch in my console application:

#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 returns kDNSServiceErr_NoError, but the callback is never called. What's wrong?

Violet Giraffe
  • 32,368
  • 48
  • 194
  • 335

2 Answers2

3

You need a main loop processing Bonjour events. Look at the link you provided carefully. It's there in the "Driving the Callbacks" section.

Patrick
  • 2,243
  • 2
  • 23
  • 32
  • 1
    "Surely, macOS is not so silly as to drive this process by polling, too." Silly, naive me. It's the same. Maybe there is a way to manage this sanely through libdispatch... – Ian Ollmann Jan 21 '23 at 22:42
1

I had to call the method named DNSServiceProcessResult to make it work for my part. I hope it helped

wscourge
  • 10,657
  • 14
  • 59
  • 80