Welcome!

AJAX & REA Authors: John Funnell, Bob Little, Kevin Hoffman, Maureen O'Gara, Onkar Singh

Related Topics: .NET

.NET: Article

Hot and Steamy Mac on Vista Action

What I really like about Bonjour

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

More Stories By Kevin Hoffman

Kevin Hoffman, editor-in-chief of SYS-CON's iPhone Developer's Journal, has been programming since he was 10 and has written everything from DOS shareware to n-tier, enterprise web applications in VB, C++, Delphi, and C. Hoffman is coauthor of Professional .NET Framework (Wrox Press) and co-author with Robert Foster of Microsoft SharePoint 2007 Development Unleashed. He authors The .NET Addict's Blog at .NET Developer's Journal.

Comments (1) View Comments

Share your thoughts on this story.

Add your comment
You must be signed in to add a comment. Sign-in | Register

In accordance with our Comment Policy, we encourage comments that are on topic, relevant and to-the-point. We will remove comments that include profanity, personal attacks, racial slurs, threats of violence, or other inappropriate material that violates our Terms and Conditions, and will block users who make repeated violations. We ask all readers to expect diversity of opinion and to treat one another with dignity and respect.


Most Recent Comments
.NET News 07/20/07 08:13:21 PM EDT

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 :)