Partager via


Net Peer TCP

Download sample

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).

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.

NoteNote:

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 PeerChannel 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 participating 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, for example:

<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 will be displayed indicating it is ready to send messages. Press ENTER to send the example chat message. This message will be 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 Procedures section describes 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

  1. Ensure that you have performed the One-Time Setup Procedure for the Windows Communication Foundation Samples.

  2. To build the C# or Visual Basic .NET edition of the solution, follow the instructions in Building the Windows Communication Foundation Samples.

  3. To run the sample in a single machine configuration, follow the instructions in Running the Windows Communication Foundation Samples.

  4. To install PNRP on Windows XP SP2 (one-time setup):

    1. In the Control Panel, double-click Add or Remove Programs.

    2. In the Add or Remove Programs dialog box, click Add/Remove Windows Components.

    3. In the Windows Components Wizard, select the "Networking Services" check box and click "Details".

    4. Check the "Peer-to-Peer" check box and click "OK".

    5. Click "Next" in the Windows Components Wizard.

    6. When the installation completes, click "Finish".

    7. From a command shell prompt, start the PNRP service with the following command: net start pnrpsvc.

  5. For this sample, wherever step 3 refers to client and service, those steps apply to sender and receiver.

  6. Start the receiver (or multiple receivers) and the sender. When the instances connect, press ENTER in the console of the sender app to send the example message to the receiver(s) to each other. Chat messages sent by sender are received by all receivers.

Footer image

Send comments about this topic to Microsoft.
© Microsoft Corporation. All rights reserved.