How to discover services

To discover bonjour services, you need 2 things. The first one is to know what kind of services you want to discover. Typically, if you want to discover daap servers (iTunes sharing librairy), you need to search for services with the protocol (daap.tcp)

If you want to discover services without timeout, here is the code :
    class Program
    {
        static void Main(string[] args)
        {
            BonjourServiceResolver bsr = new BonjourServiceResolver();
            bsr.ServiceFound += new Network.ZeroConf.ObjectEvent<Network.ZeroConf.IService>(bsr_ServiceFound);
            bsr.Resolve("_daap._tcp");
            Console.ReadLine();
        }

        static void bsr_ServiceFound(Network.ZeroConf.IService item)
        {
			// Here goes the code for what you want to do when a service is discovered
            Console.WriteLine(item);
        }
    }

If you want to discover services with timeout. That's to say a blocking resolve, you need to specify who long you want to wait, how much services must be found (at least/at most).
Here is the code to do so :
    class Program
    {
        static void Main(string[] args)
        {
            BonjourServiceResolver bsr = new BonjourServiceResolver();
            bsr.ServiceFound += new Network.ZeroConf.ObjectEvent<Network.ZeroConf.IService>(bsr_ServiceFound);
			//Here, we look for the services : 
			// - implementing the daap protocol
			// - for 30s
			// - at least one service
			// - at most 10 services.
            bsr.Resolve("_daap._tcp", new TimeSpan(0,0,30), 1, 10);
            Console.ReadLine();
        }

        static void bsr_ServiceFound(Network.ZeroConf.IService item)
        {
            Console.WriteLine(item);
        }
    }

Last edited Feb 12, 2009 at 1:28 PM by neonp, version 1

Comments

orinoco77 Feb 2, 2014 at 10:48 AM 
Tried the same code with Xamarin Studio and Mono 3.3.0 and had exactly the same result. Any idea what's wrong?

orinoco77 Feb 2, 2014 at 10:29 AM 
I've just followed this example in VS2013 and I get an exception on the bsr.Resolve line:

System.IO.EndOfStreamException occurred
Message: A first chance exception of type 'System.IO.EndOfStreamException' occurred in mscorlib.dll
Additional information: Unable to read beyond the end of the stream.

Code is exactly as above. Is this related to VS2013? Should I be using an earlier version?

Nuno35242 Oct 23, 2013 at 2:41 PM 
I try this code with two ethernet cards (interfaces) and the message is sent only on one of them. If I try to send the multicast message over the interface choosed by me on server.Bind() function the message is sent and I get response, but the function ReceiveRequest doesn't fires. I know there I'm receiving data because I use wireshar and i see that data. This approach must send data over all the network interfaces and receive from all network interfaces. I can't understand why the multicast message is sent only on one of the network interfaces.

Thanks in advance,

Nuno Peixoto