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

9/16/2009 8:29:00 AM

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:

View the Screencast:

Download the Demo Code

Tags:

Peer2Peer

Comments

9/26/2009 2:10:55 PM

Ing

Cool website!! Thanks for info!!!

Ing United States

Comments are closed

Powered by BlogEngine.NET 1.5.0.7
Theme by Mads Kristensen

About the author

Jeff Brand Jeff Brand

This is the personal web site of Jeff Brand, self-proclaimed .NET Sex Symbol and All-Around Good guy. Content from my presentations, blog, and links to other useful .NET information can all be found here.

E-mail me Send mail


Calendar

<<  March 2010  >>
MoTuWeThFrSaSu
22232425262728
1234567
891011121314
15161718192021
22232425262728
2930311234

View posts in large calendar

My Twitter Updates

XBOX
Live

Recent comments

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2010

Sign in