The World’s Leading Microsoft .NET Magazine
   
 
The .NET Addict's Blog

My Top Tags

                                                           

My RSS Feeds








I heart FeedBurner

Latest Diggs - Programming

Computers Blogs - Blog Top Sites

Site Hits

Total: 2,817,121
since: 19 Jan 2005

Hot and Steamy Mac on Vista Action!!

posted Wed 18 Jul 07

In homage to one of the best subject lines I've seen in my spam-filtered trash box in a long time, I decided to write a post on doing some cross-platform dynamic service discovery using Zeroconf. Most of the subject lines of the spam that I get aren't related to Macs or Vista, but they're funny nonetheless. It is basically a multicast DNS protocol that provides for dynamic service publication and discovery. You've probably heard of various implementations of Zeroconf as Bonjour (formerly Rendezvous) and Avahi.

What I really like about Bonjour isn't that you can use it to discover nearby iTunes libraries for music sharing (that's how iTunes actually does use Bonjour) or that you can use Bonjour to discover nearby printers (also a legitimate use, that's how I discovered my HP scanner-fax-printer from my Mac) - it's the fact that you can use Bonjour to dynamically publish the location of services within your enterprise, including arbitrary metadata about those services. To me, this is far more compelling than the iTunes scenario. UDDI is such a huge overbloated pain in the ass, and it's not flexible or dynamic enough for me, and as a propogator of WSDL, UDDI is my enemy :)

So, I aquired a Zeroconf .NET library from this web site. This library is a wrapper for Apple's Bonjour. Basically all you need to do is first install Bonjour for Windows and this .NET library works out of the box. Take a look at how easy it is to publish the type, name, and location of a service as well as arbitrary metadata:

publishService = new NetService("", "_kevin._tcp",
  "Kevin's Hot Service", 80);
System.Collections.Hashtable dict = new System.Collections.Hashtable();
byte[] byte1 = Encoding.UTF8.GetBytes("record1");
byte[] byte2 = Encoding.UTF8.GetBytes("and another record.");

dict.Add("data1", byte1);
dict.Add("data2", byte2);

publishService.TXTRecordData = NetService.DataFromTXTRecordDictionary(dict);
publishService.Publish();

Now that I'm publishing the location of my hot service (I am sure there are countless hot and steamy things that can be done with said service, just like all the spams say!) I can switch over to the Mac and use some Cocoa to browse for services of type "_kevin._tcp" ... which will give me a list of all instances of my ridiculously cool service on the LAN and allow me to resolve those to addresses and ports.

Without having to aquire another library for Bonjour (support for it is built into Cocoa), I can quickly create a controller that requests a browse for services of a given type and receives notifications about hits through the (hopefully well-known if you read my blog) delegate pattern:

-(IBAction)startBrowsing:(id)sender
{
    browser = [[NSNetServiceBrowser alloc] init];
    [browser setDelegate:self];
    [browser searchForServicesOfType:@"_kevin._tcp" inDomain:@"local"];
}
#pragma mark browser delegate
- (void)netServiceBrowser:(NSNetServiceBrowser *)aNetServiceBrowser
    didRemoveService:(NSNetService *)aNetService
    moreComing:(BOOL)moreComing
{
    [servicesArray removeObject:aNetService];
}

- (void)netServiceBrowser:(NSNetServiceBrowser *)aNetServiceBrowser
    didFindService:(NSNetService *)aNetService
    moreComing:(BOOL)moreComing
{
    [servicesArray addObject:aNetService];
}

And without further delay, here's a screenshot of my Mac client using Bonjour to discover a service published by a .NET Framework 3.0 Windows Vista application through a .NET wrapper around Bonjour for Windows:

Pretty steamy, huh?

tags:          

links: digg this    del.icio.us    technorati    reddit




Tag Related Posts

Leopard Code Sample : Sprinkling in some Bonjour

Tue 27 Nov 07 2:32 P GMT-05
tags:        

My TiVo is using Bonjour...

Mon 18 Jun 07 12:57 A GMT-05
tags:    

Orcas' Hidden Gem - The managed PNRP stack

Fri 11 May 07 6:45 P GMT-05
tags: