4

I am developing a program that uses Bonjour algorithm to find services in network. I downloaded Bonjour SDK for Windows. There were two sample applications on .Net. Both are Windows Forms project. Samples work fine but when I tried to implement sample console application I received AccessViolationException. Here is my code:

class Program
{
    static void Main(string[] args)
    {
        DNSSDService service = new DNSSDService();
        DNSSDEventManager eventManager = new DNSSDEventManager();
        eventManager.ServiceFound += new _IDNSSDEvents_ServiceFoundEventHandler(eventManager_ServiceFound);
        //next line raises AccessViolationException
        DNSSDService browse = service.Browse(0, 0, "_psia._tcp", null, eventManager);

        Console.ReadKey();
    }

    static void eventManager_ServiceFound(DNSSDService browser, DNSSDFlags flags, uint ifIndex, string serviceName, string regtype, string domain)
    {
        //TODO: some logic
    }
}

I found the solution - all methods from Bonjour library should be called from STA thread. So I added [STAThread] attribute to Main method. The code now looks like this:

class Program
{
    [STAThread]
    static void Main(string[] args)
    {
        DNSSDService service = new DNSSDService();
        DNSSDEventManager eventManager = new DNSSDEventManager();
        eventManager.ServiceFound += new _IDNSSDEvents_ServiceFoundEventHandler(eventManager_ServiceFound);
        DNSSDService browse = service.Browse(0, 0, "_psia._tcp", null, eventManager);

        Console.ReadKey();
    }

    static void eventManager_ServiceFound(DNSSDService browser, DNSSDFlags flags, uint ifIndex, string serviceName, string regtype, string domain)
    {
        //TODO: some logic
    }
}

I solved the issue with AccessViolationException but received a new one. After I call method Browse, 'ServiceFound' event should be called for each service defined in network. But it did not. After some time of investigation I found that I need to run message pump to receive all these events. So the final code looks like this:

 class Program
{
    [STAThread]
    static void Main(string[] args)
    {
        DNSSDService service = new DNSSDService();
        DNSSDEventManager eventManager = new DNSSDEventManager();
        eventManager.ServiceFound += new _IDNSSDEvents_ServiceFoundEventHandler(eventManager_ServiceFound);
        DNSSDService browse = service.Browse(0, 0, "_psia._tcp", null, eventManager);

        Application.Run();
    }

    static void eventManager_ServiceFound(DNSSDService browser, DNSSDFlags flags, uint ifIndex, string serviceName, string regtype, string domain)
    {
        //TODO: some logic
    }
}

To stop message loop call Application.Exit().

uzrgm
  • 375
  • 3
  • 8
  • You helped me a lot!!!! Do you know how to get the ip address and port in the:`eventManager_ServiceFound` ? – Yoda Jan 11 '14 at 15:24

1 Answers1

0

I tried to use your code to discover ip camera ip address, but I do not know what hostName I should put to make GetAddrInfo work.

using Bonjour;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace WindowsFormsApplication1 {
    static class Program {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main() {
            DNSSDService service = new DNSSDService();
            DNSSDEventManager eventManager = new DNSSDEventManager();
            eventManager.ServiceFound += new _IDNSSDEvents_ServiceFoundEventHandler(eventManager_ServiceFound);
         //   DNSSDService browse = service.Browse(0, 0, "_axis-video._tcp", null, eventManager);
            DNSSDService browse = service.Browse(0, 0, "_http._tcp", null, eventManager);


            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
        static void eventManager_ServiceFound(DNSSDService browser, DNSSDFlags flags, uint ifIndex, string serviceName, string regtype, string domain) {
            Console.WriteLine("browser: " + browser + "\nDNSSDFlags " +  flags+ "\nifIndex " + ifIndex +"\nserviceName: " + serviceName + "\nregtype: " +regtype+ "\ndomain: "+ domain);

            DNSSDEventManager eventManager = new DNSSDEventManager();
            eventManager.AddressFound += new _IDNSSDEvents_AddressFoundEventHandler(eventManager_AddressFound);
            DNSSDAddressFamily family = new DNSSDAddressFamily();
            browser.GetAddrInfo(flags, ifIndex, family, "axis-00408cbeeae5", eventManager);

        }

        private static void eventManager_AddressFound(DNSSDService service, DNSSDFlags flags, uint ifIndex, string hostname, DNSSDAddressFamily addressFamily, string address, uint ttl) {
            Console.WriteLine("----------------------------------------");
            Console.WriteLine("FFFFFFFFFFFFFFFFFFFFFOUUUUUUUUUUUUUUUUND");
            Console.WriteLine("----------------------------------------");

        }



    }
}
Yoda
  • 17,363
  • 67
  • 204
  • 344