Peer to Peer Series Part 2: Registering Names with PNRP API
- Posted: Sep 17, 2009 at 4:55 PM
- 11,602 Views
Right click “Save as…”
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:
Comments have been closed since this content was published more than 30 days ago, but if you'd like to continue the conversation,
please create a new thread in our Forums,
or
Contact Us and let us know.
Follow the Discussion
Oops, something didn't work.
What does this mean?
Following an item on Channel 9 allows you to watch for new content and comments that you are interested in. You need to be signed in to Channel 9 to use this feature.What does this mean?
Following an item on Channel 9 allows you to watch for new content and comments that you are interested in and view them all on your notifications page.sign up for email notifications?