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

Download this episode

Download Video

Description

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

Embed

Format

Available formats for this video:

Actual format may change based on video formats available and browser capability.

    The Discussion

    Comments closed

    Comments have been closed since this content was published more than 30 days ago, but if you'd like to send us feedback you can Contact Us.