Net Peer TCP
This sample demonstrates using the NetPeerTcpBinding binding, which provides multiparty communication using a peer channel. This sample is a variation of the Getting Started Sample. Refer to Getting Started Sample for an overview of Windows Communication Foundation (WCF).
Note
The WCF samples may already be installed on your machine. Check this (default) directory before continuing: <InstallDrive>:\Samples\WCFWFCardspaceIf this directory doesn’t exist, click the download sample link at the top of this page. Note that this will download and install all of the WF, WCF, and CardSpace samples, you will only have to do this once. The sample is located in the following directory <InstallDrive>:\Samples\WCFWFCardSpace\WCF\Basic\Binding\Net\PeerTcp\Default.
In this sample, the application instances are self-hosted console applications.
Unlike other transport binding samples, this sample uses a custom contract interface (IBroadcast
) for the purpose of illustrating multiparty communication. All instances implement this contract to receive messages and create proxies with the same contract to send messages to the mesh. This is demonstrated by creating a duplex channel to the mesh.
Note
The setup procedure and build instructions for this sample are located at the end of this topic.
Understanding the binding configuration process in the sample involves the following peer channel concepts:
The peer resolver is responsible for resolving a mesh ID to the endpoint addresses of a few nodes in the mesh.
A mesh is a named collection of peer nodes identified by the mesh ID.
A peer node is an instance of an application that participates in the mesh.
Mesh IDs identify the host portion of the address of an endpoint in the mesh. Examples of these addresses are "net.p2p://chatMesh/servicemodelsamples/chat" or "net.p2p://broadcastMesh/servicemodelsamples/announcements". chatMesh and broadcastMesh are the mesh IDs.
All clients that participate in a mesh use the same mesh ID, but can potentially use different paths and services. A message addressed to a specific endpoint address is delivered to all peer channels using that address.
When a peer node is opened (as a result of opening the peer channel), it uses a peer resolver to resolve mesh ID to the addresses of a few other peer nodes to connect to. This creates a mesh of interconnected nodes and enables messages to be propagated throughout the mesh.
The binding is specified in the configuration files of the sender and the receivers. The binding type is specified in the endpoint element’s binding attribute as shown in the following sample.
<client>
<endpoint name="BroadcastEndpoint"
address=
"net.p2p://broadcastMesh/servicemodelsamples/announcements"
binding="netPeerTcpBinding"
bindingConfiguration="Binding1"
contract="Microsoft.ServiceModel.Samples.IBroadcast">
</endpoint>
</client>
If you use NetPeerTcpBinding binding with the default behavior, password-based security is enabled. The binding element provides attributes for setting port, listen IP address, resolver type, maximum message size, maximum buffer pool size, reader quotas, peer node authentication mode, message authentication, and timeouts (for close, open, send, and receive).
Note
This sample uses the default peer resolver (PNRP), which is not available in Windows Server 2003. Therefore, to run this example in Windows Server 2003, you must use a custom peer resolver. Please refer to Peer Channel Chat for a sample that uses a custom peer resolver, as shown in the following code.
<netPeerTcpBinding>
<binding configurationName="Binding1">
<resolver mode="Custom">
<customResolver type=
"MyAppNameSpace.MyCustomPeerResolver, myApp"/>
</resolver>
</binding>
</netPeerTcpBinding>
The file that contains MyCustomPeerResolver
must be compiled with the sender and the receivers. Note that if the sample is being run on multiple machines with different platforms, they should all use the same resolver.
The receiver and sender implementations also demonstrate how to retrieve the peer node associated with the receiver or sender instance and to register for its online and offline events. An online event is initiated when the peer node is connected to at least one other peer node in the mesh. An offline event is initiated when a peer node is no longer connected to any other peer node in the mesh.
At this time, the peer channel does not integrate with the Service Model Metadata Utility Tool (Svcutil.exe). For this reason, Svcutil.exe cannot be utilized to generate a typed channel for the sender.
When you run the sender, a message is displayed that indicates it is ready to send messages. Press ENTER to send the example chat message. This message is displayed in the any currently running receiver client console windows. To terminate the clients, press ENTER again in the console windows of that client (sender or receiver).
If you enable tracing or message logging, you can monitor the sender and receiver activity at a deeper level. The following procedures describe how to enable tracing and message logging.
Note
It is important to note that the sample currently does not handle all possible exceptions that the infrastructure may throw. If you are using these samples in a commercial or production environment, please follow the correct exception handling best practices.
To set up, build, and run the sample
Ensure that you have performed the One-Time Set Up Procedure for the Windows Communication Foundation Samples.
To build the C# or Visual Basic .NET edition of the solution, follow the instructions in Building the Windows Communication Foundation Samples.
To run the sample in a single machine configuration, follow the instructions in Running the Windows Communication Foundation Samples.
PNRP is available in Windows Vista, Windows XP SP3, and Windows Server 2008. For Windows XP SP2, please follow the one-time setup instructions:
In the Control Panel, double-click Add or Remove Programs.
In the Add or Remove Programs dialog box, click Add/Remove Windows Components.
In the Windows Components Wizard, select the "Networking Services" check box and click "Details".
Check the "Peer-to-Peer" check box and click "OK".
Click "Next" in the Windows Components Wizard.
When the installation completes, click "Finish".
From a command shell prompt, start the PNRP service with the following command: net start pnrpsvc.
For this sample, wherever step 3 refers to client and service, those steps apply to sender and receiver.
Start the receiver (or multiple receivers) and the sender. When the instances connect, press ENTER in the console of the sender application to send the example message to the receiver(s) to each other. Chat messages sent by sender are received by all receivers.
© 2007 Microsoft Corporation. All rights reserved.