Peer to Peer Series Part 3: Resolving Names Synchronously with the PNRP API

Download

Right click “Save as…”

  • WMV (WMV Video)
  • MP3 (Audio only)
  • MP4 (iPhone, Android)
  • Mid Quality WMV (Lo-band, Mobile)

In Part 3 of the Peer to Peer Series, I add another Command Prompt application to the demo solution and use the PNRP API to resolve a Peer Name using C#.  As with registering a Peer Name, resolving Peer Names is straightforward:

PeerNameResolver resolver = new PeerNameResolver();

try
{
records = resolver.Resolve(new PeerName(classifier, PeerNameType.Unsecured));

if (records.Count > 0)
{
foreach (var record in records)
{
Console.WriteLine("***Peer: {0},record.PeerName.ToString());
Console.Writeline("***Host Name: {0}, record.PeerName.PeerHostName);
Console.Writeline("***Comment: {0}", record.Comment);
foreach (var endpoint in record.EndPointCollection)
Console.WriteLine("\tEndpoint: {0}, Port: {1}", endpoint.Address.ToString(), endpoint.Port);

Console.WriteLine();
}
}
}
catch (PeerToPeerException ex)
{
//Other standard execptions can also be caught
Console.WriteLine("PeerToPeer Excpetion: {0)", ex.Message);
}


One of the interesting things about resolving a Peer Name is that the resulting PeerNameRecords include the endpoint information for each of the Peer Names found.  This means you can establish direct connections with those peers if necessary.  I will show how to do just that in an upcoming screencast.  Of course, that assumes we don’t have any firewalls or other types of security blockades preventing direct communication.

The example in this installment is done synchronously.  In the next screencast, I will show you how you can do the same operation asynchronously.  This is useful because the Resolve method can take quite a while to return so your program’s execution is blocked while waiting for the method to complete.  This is especially bad in the case of GUI applications since the app will become non-responsive while waiting for Resolve to finish.

Other documentation for PNRP:


Download the Demo Code

Tags:

Follow the Discussion

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.