Peer to Peer Series Part 2: Registering Names with PNRP API
- Posted: Sep 17, 2009 at 4:55 PM
- 11,249 Views
Download
How do I download the videos?
- To download, right click the file type you would like and pick “Save target as…” or “Save link as…”
Why should I download videos from Channel9?
- It's an easy way to save the videos you like locally.
- You can save the videos in order to watch them offline.
- If all you want is to hear the audio, you can download the MP3!
Which version should I choose?
- If you want to view the video on your PC, Xbox or Media Center, download the High Quality WMV file (this is the highest quality version we have available).
- If you'd like a lower bitrate version, to reduce the download time or cost, then choose the Medium Quality WMV file.
- If you have a Zune, WP7, iPhone, iPad, or iPod device, choose the low or medium MP4 file.
- If you just want to hear the audio of the video, choose the MP3 file.
Right click “Save as…”
- MP3 (Audio only)
- MP4 (iPod, Zune HD)
- Mid Quality WMV (Lo-band, Mobile)
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 Closed
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.