Peer to Peer Series Part 2: Registering Names with PNRP API

9/15/2009 12:59:00 PM

In Part Two of the Peer to Peer Screencast series by SlickThought Productions, we look at using C# and the PNRP API to register a peer name.  This example uses a simple Command Prompt application to register the Peer Name, but the same techniques can be used to register a Peer Name from a WinForm or WPF application.  It could even be used inside a Windows Service which opens up some interesting scenarios as well.

Using the API is very simple.  Here is the main code section from the demo:

PeerName peerName = new PeerName(classifier, PeerNameType.Secured);
 
using (PeerNameRegistration registration = new PeerNameRegistration(peerName, 8080))
{
    try
    {
        registration.Start();
        // Do stuff
        registration.Stop();
 
    }
    catch (PeerToPeerException ex)
    {
        // There are other possible statndard exceptions to catch 
        // See documentation on for details
    }

 

Pretty straightforward.  One of the interesting things you can do with a Peer Name is add a Comment and Data to the registration.  Doing so is straightforward:

registration.Comment = "My Comment";

UnicodeEncoding encoder = new UnicodeEncoding();
byte[] data = encoder.GetBytes("Some Data");
registration.Data = data;

In the snippet above, I am adding a simple string to the Comment property.  For the Data property, I am converting a simple string to an array of bytes and passing that to the property.  In the case of the Data property, you can pass in any byte array you want, so instead of a string I could have passed in an image, a data blob, whatever.  In the case of the Comment property, you are limited to 39 Unicode characters, and the Data property is limited to 4,096 bytes.

The important thing to remember when working with PNRP and registering a Peer Name is that the process that called the Start method must remain open.  As soon as that process shuts down, the Peer Name registration is lost.

Other documentation for PNRP:

View the Screencast:

Download the Demo Code

Tags:

Peer2Peer

Comments are closed

Powered by BlogEngine.NET 1.6.0.0
Theme by Mads Kristensen

About the author

Jeff Brand Jeff Brand

This is the personal web site of Jeff Brand, self-proclaimed .NET Sex Symbol and All-Around Good guy. Content from my presentations, blog, and links to other useful .NET information can all be found here.

E-mail me Send mail


Calendar

<<  September 2010  >>
MoTuWeThFrSaSu
303112345
6789101112
13141516171819
20212223242526
27282930123
45678910

View posts in large calendar

My Twitter Updates

XBOX
Live

Recent comments

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2010

Sign in