Peer to Peer Series Part 7: PeerChannel Basics

12/11/2009 8:42:00 AM

In Part 7, I introduce the basics of building a WCF PeerChannel application.  Using a simple console application, the following steps are shown:

  • Creating the necessary interfaces
  • Setting up the WCF environment
  • Checking online status
  • Sending and receiving messages

While basic, it demonstrates the core steps used in PeerChannel applications.  It also sets the stage for a more detailed look at the Buzzword Bingo application shown in Part 6.

View the Screencast:

Tags:

Peer2Peer

Peer to Peer Series Part 6: Introduction to PeerChannel

11/10/2009 11:07:00 AM

In Part 6 of the Peer to Peer Series, I set the stage for learning how to use WCF's PeerChannel.  After a simple overview of PeerChannel, I show a sample application that uses PeerChannel to play buzzword bingo.  The focus of this installment is to give an understanding of some of the key considerations one must think about dealing with the message broadcasts that comprise a PeerChannel application.

The next few installments will focus extensively on building peer applications using PeerChannel.

Other documentation for PNRP:

View the Screencast:

Tags:

Peer2Peer

Peer to Peer Presentation from Code Camp Available

10/27/2009 9:01:01 AM

For those of you that want the Code Camp presentation, you can get it here.  The code can be downloaded here.

Tags:

Peer2Peer

Peer to Peer Series Part 5: Connecting Peers via WCF

10/8/2009 11:48:00 AM

In Part 5 of the Peer to Peer Series, I show a sample application that uses PNRP to connect a client application to a server application using WCF and PNRP Peer Host Names. The details of the actual WCF Service implementation.  Instead, I will focus on those areas of leveraging PNRP to establish connections via WCF.

The video will show a sample application that is comprised of a client and a server application.  You could use a more “peer-to-peer” duplex-style of communication, but the client-server arrangement makes the two key parts of establishing a WCF connection easier to explain.

When hosting a WCF service, the key code from the IntelServiceHost.cs file is shown below:

 

this.PeerRegistration.Start(peerClassifier, port);
Uri tcpUri = new Uri(string.Format("net.tcp://{0}:{1}/IntelService", this.PeerRegistration.PeerUri, port));
_serviceHost = new ServiceHost(service, tcpUri);
NetTcpBinding tcpBinding = new NetTcpBinding(SecurityMode.None);
_serviceHost.AddServiceEndpoint(typeof(IIntelService), tcpBinding, "");
 

The PeerRegistration property exposes a wrapper class around the PNRP API.  The Start method registers the application with a given peer classifier and port as an unsecure peer name in the Global PNRP Cloud.  The PeerUri property passes back the peerhostname.  That uri is used to setup a new Uri, along with the port, that is then used to configure the ServiceHost using a NetTcpBinding.  Some of this could have been configured in the application’s config file, but I put it all here so you can see how the different parts relate.

The client side of the equation is even easier (found in the IntelClient.cs file):

PeerNameResult peerRecord = PeerResolution.ResolveHostName(hostPeerName);
 
System.ServiceModel.Channels.Binding netBinding = new NetTcpBinding(SecurityMode.None);
EndpointAddress endpointAddress = new EndpointAddress(string.Format("net.tcp://{0}:{1}/IntelService", peerRecord.Uri, peerRecord.Port));
 
IntelServiceProxy = new IntelProxy(netBinding, endpointAddress);

The PeerResolution class is another wrapper around the PNRP API, this time providing resolving a given unsecure peer name in the Global PNRP cloud.  It returns an object containing the remote peer’s peerhostname and port. The rest of the code is just setting up the client side of the WCF connection.

Other documentation for PNRP:

View the Screencast:

Download the Demo Code

Tags:

Peer2Peer

Peer to Peer Series: Sample WPF PNRP Application

9/18/2009 10:52:00 AM

PNRP Building on the examples for the first four installments, I whipped up a little WPF app that uses PNRP.  You can create registrations and look them up.  It is a good example of using the various features of PNRP to form the foundation of a P2P application. I banged this app out pretty quickly so no snarking on things that may be incorrect, designed poorly, whatever.

Download PeerToPeer Code Here

Tags:

Peer2Peer

Peer to Peer Series Part 4: Resolving Peer Names Asynchronously

9/16/2009 9:08:00 AM

In Part 4 of the Peer to Peer Series, I modify the Resolve program to find Peer Names using the asynchronous methods.  Below is a very simple example of using the ResolveAsync method:

PeerNameResolver resolver = new PeerNameResolver();
 
try
{
    resolver.ResolveCompleted += new EventHandler<ResolveCompletedEventArgs>(resolver_ResolveCompleted);
    resolver.ResolveAsync(new PeerName(classifier, PeerNameType.Secured), Guid.NewGuid());
}
catch (PeerToPeerException ex)
{
    // There are other standard exceptions you can also catch - see docs
    Console.WriteLine("PeerToPeer Excpetion: {0)", ex.Message);
}
 
static void resolver_ResolveCompleted(object sender, ResolveCompletedEventArgs e)
{
    if (!e.Cancelled && e.Error == null && e.PeerNameRecordCollection != null)
    {
        records = e.PeerNameRecordCollection;
        DisplayResults();
    }
}

There is also a ResolveProgress event you can also wire up.  As the name implies, it will fire periodically to indicate the progress being made in resolving the Peer Name.  It was not overly useful when resolving a Peer Name that had a small number of results, but your mileage may vary.

The one thing that is also a “brute force” implementation in the demo code is how I pass a Guid.NewGuid() into the ResolveAsync method.  The Guid is being used as the UserState parameter in the method, and can actually be any Object. In a real implementation where you may have multiple ResovleAsync requests in-flight at any one time, you would want to implement a tracking system using this UserState parameter so  you could match up the corresponding ResolveCompleted event with the correct initiating request.  The ResolveCompleteEventArgs parameter contains the UserState used to initiate the request, so the matching is pretty straightforward.  I did not go the extra mile and implement that since the demo only fired a single ResolveAsync request.  You cannot pass in a null UserState object, so I went with a simple Guid for no particularly good reason.

Other documentation for PNRP:

View the Screencast:

Download the Demo Code

Tags:

Peer2Peer

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

Peer to Peer Series Part 2: Registering Names with PNRP API

9/15/2009 12:59:00 PM

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:

View the Screencast:

Download the Demo Code

Tags:

Peer2Peer

Peer to Peer Series Part 1: Intro to PNRP

9/15/2009 9:44:00 AM

Intro to PNRP This kick-off screencast introduces you to the foundation of Peer-to-Peer applications, PNRP.  Peer Name Resolution Protocol is the the underlying mechanism used by applications to discover peers on a network.  This can either be a local subnet or the Internet at large.  One of the great things about PRNP is that is even works across NAT devices.

In this screencast, I introduce you some fundamental PNRP concepts and show how you can use the command line to test out the basics.  This screencast is not intended to be a deep dive technical exploration.  Rather, it lays a sufficient foundation for you to understand what is going on in future screencasts and lets you get started building P2P applications as soon as possible.

If you are interested in more technical information on PRNP, check out the following links:

You can watch the first installment of the P2P series using the links below:

Tags:

Peer2Peer

Journey Into Peer-to-Peer Begins

6/12/2009 4:07:00 PM

With some recent announcements from a big Internet company whose name cannot be mentioned, I went off on my own expedition into the dark corners of the .NET and Windows platform to see what cool technology treasures may be lurking there.  If you are like me, you may have heard something about P2P technologies being delivered for Windows.  And if you were like me, you didn’t really pay much attention to it at the time since only file sharing, music stealing people cared about peer to peer technologies.

Well, let me tell you, my friend, that was a very wrong assumption!  There is a ton of amazing P2P technologies in Windows and .NET, and they make the creation of amazing new applications possible.  Ever had an idea where your app would need to dynamically find another computer and communicate with it but both computers might be around the world, not registered with DNS, and hiding behind a NAT device? PNRP in Windows makes that easy! Every thought of having a global hashtable to store all kinds of data that could be retrieved by your app’s users – DRT to the rescue!

What are PNRP and DRT you ask?  That, dear reader, will be what I will cover in a series of blog posts and screencasts in the coming weeks to show you how to leverage and exploit this cool technology.  Want to get a head start?  Check out the P2P team’s blog and the Microsoft Peer-to-Peer Reference page.

Tags:

Peer2Peer

Powered by BlogEngine.NET 1.6.0.0
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

<<  August 2014  >>
MoTuWeThFrSaSu
28293031123
45678910
11121314151617
18192021222324
25262728293031
1234567

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 2014

Sign in