Peer to Peer Series Part 2: Registering Names with PNRP API
- Posted: Sep 17, 2009 at 4:55PM
- 12,290 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))
// Do stuff
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: