Bug 18734 - NSNetService and NSNetServiceBrowser on a Mac doesn't work.
Summary: NSNetService and NSNetServiceBrowser on a Mac doesn't work.
Status: RESOLVED ANSWERED
Alias: None
Product: Xamarin.Mac
Classification: Desktop
Component: Library (Xamarin.Mac.dll) ()
Version: Master
Hardware: Macintosh Mac OS
: High normal
Target Milestone: 2.4.0 (C6)
Assignee: Chris Hamons
URL:
Depends on:
Blocks:
 
Reported: 2014-04-02 08:17 UTC by Ken
Modified: 2015-03-17 12:13 UTC (History)
5 users (show)

Tags:
Is this bug a regression?: ---
Last known good build:


Attachments
Xcode sample (32.64 KB, application/zip)
2015-03-17 12:13 UTC, Timothy Risi
Details


Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED ANSWERED

Description Ken 2014-04-02 08:17:29 UTC
I am using the following code.

using System;
using MonoMac.Foundation;   // <-- Xamarin.Mac
using System.Threading;

namespace BonjourTest
{
	static class TestService
	{
		static NSNetService _testService;

		public static void InitTestService()
		{
			_testService = new NSNetService ("", "_testservice._tcp.", "Test Service", 28132);

			_testService.Published += delegate(object sender, EventArgs e) {
				NSNetService ns = sender as NSNetService;
				Console.WriteLine("Bonjour published service '{0}'", ns.Name);
			};
			_testService.WillPublish += delegate(object sender, EventArgs e) {
				NSNetService ns = sender as NSNetService;
				Console.WriteLine("Bonjour will publish service '{0}'", ns.Name);
			};
			_testService.PublishFailure += delegate(object sender, NSNetServiceErrorEventArgs e) {
				NSNetService ns = sender as NSNetService;
				Console.WriteLine("Bonjour failed to publish service '{0}'", ns.Name);
			};

			_testService.Publish ();
		}
	}

	class MainClass
	{
		static NSNetServiceBrowser _bonjourBrowserService;

		static void InitBonjourBrowser()
		{
			_bonjourBrowserService = new NSNetServiceBrowser ();

			_bonjourBrowserService.FoundService += delegate(object sender, NSNetServiceEventArgs e) {
				Console.WriteLine("Bonjour found service '{0}'", e.Service.Name);
			};
			_bonjourBrowserService.ServiceRemoved += delegate(object sender, NSNetServiceEventArgs e) {
				Console.WriteLine("Bonjour removed service '{0}'", e.Service.Name);
			};
			_bonjourBrowserService.SearchStarted += delegate(object sender, EventArgs e) {
				Console.WriteLine("Bonjour starting search");
			};
			_bonjourBrowserService.SearchStopped += delegate(object sender, EventArgs e) {
				Console.WriteLine("Bonjour stopped searching");
			};
			_bonjourBrowserService.NotSearched += delegate(object sender, NSNetServiceErrorEventArgs e) {
				Console.WriteLine("Bonjour not searched");
			};

			_bonjourBrowserService.SearchForServices ("_testservice._tcp.", "");
		}

		static void Main (string[] args)
		{
			InitBonjourBrowser ();

			TestService.InitTestService ();

			while (true) {
				Thread.Sleep (1000);
			}
		}
	}
}

Results 

  Bonjour starting search
  Bonjour will publish service 'Test Service'

Expect to see

  Bonjour starting search
  Bonjour will publish service 'Test Service'
  Bonjour published service 'Test Service'
  Bonjour found service 'Test Service'

When I run basically the same code (had to change the main code, the using's and references) on an iOS 7.1 iPad device I see the expected results.

using System;
using System.Collections.Generic;
using System.Linq;
using MonoTouch.Foundation;   // <-- Xamarin.ios
using MonoTouch.UIKit;

namespace iPadBonjourTest
{
	static class TestService
	{
		static NSNetService _testService;

		public static void InitTestService()
		{
			_testService = new NSNetService ("", "_testservice._tcp", "Test Service", 28132);

			_testService.Published += delegate(object sender, EventArgs e) {
				NSNetService ns = sender as NSNetService;
				Console.WriteLine("Bonjour published service '{0}'", ns.Name);
			};
			_testService.WillPublish += delegate(object sender, EventArgs e) {
				NSNetService ns = sender as NSNetService;
				Console.WriteLine("Bonjour will publish service '{0}'", ns.Name);
			};
			_testService.PublishFailure += delegate(object sender, NSNetServiceErrorEventArgs e) {
				NSNetService ns = sender as NSNetService;
				Console.WriteLine("Bonjour failed to publish service '{0}'", ns.Name);
			};

			_testService.Publish ();
		}
	}

	public class Application
	{
		static NSNetServiceBrowser _bonjourBrowserService;

		static void InitBonjourBrowser()
		{
			_bonjourBrowserService = new NSNetServiceBrowser ();

			_bonjourBrowserService.FoundService += delegate(object sender, NSNetServiceEventArgs e) {
				Console.WriteLine("Bonjour found service '{0}'", e.Service.Name);
			};
			_bonjourBrowserService.ServiceRemoved += delegate(object sender, NSNetServiceEventArgs e) {
				Console.WriteLine("Bonjour removed service '{0}'", e.Service.Name);
			};
			_bonjourBrowserService.SearchStarted += delegate(object sender, EventArgs e) {
				Console.WriteLine("Bonjour starting search");
			};
			_bonjourBrowserService.SearchStopped += delegate(object sender, EventArgs e) {
				Console.WriteLine("Bonjour stopped searching");
			};
			_bonjourBrowserService.NotSearched += delegate(object sender, NSNetServiceErrorEventArgs e) {
				Console.WriteLine("Bonjour not searched");
			};

			_bonjourBrowserService.SearchForServices ("_testservice._tcp", "");
		}

		// This is the main entry point of the application.
		static void Main (string[] args)
		{
			InitBonjourBrowser ();

			TestService.InitTestService ();

			// if you want to use a different Application Delegate class from "AppDelegate"
			// you can specify it here.
			UIApplication.Main (args, null, "AppDelegate");

		}
	}
}

...and the results...

2014-04-02 07:05:05.096 iPadBonjourTest[5436:70b] Bonjour starting search
2014-04-02 07:05:05.101 iPadBonjourTest[5436:70b] Bonjour will publish service 'Test Service'
2014-04-02 07:05:05.230 iPadBonjourTest[5436:70b] Bonjour found service 'Test Service'
2014-04-02 07:05:05.487 iPadBonjourTest[5436:70b] Bonjour published service 'Test Service'
Comment 3 Timothy Risi 2015-03-16 20:43:35 UTC
I tried writing this same sample in Xcode in objective-c and got the exact same result as the Xamarin.Mac version, so it looks like either an Apple issue
Comment 4 Timothy Risi 2015-03-17 12:13:22 UTC
Created attachment 10382 [details]
Xcode sample