Using WCF NetPeerTcpBinding
we can develop a peer-to-peer networking applications that use a TCP-level peer-to-peer
mesh infrastructure.
Example for the Peer-to-Peer Application: IM, File
Sharing Application, Games etc.
In Traditional Client Server model we have single
and 1 to N number of clients making
request to the server which is configured in centralized location.
In Traditional Client/Server application.
In peer to peer (p2p) application each participate (node) acts as both a
client and a server to the other participants in the network.
In peer – to- peer network environment peers rely on name
resolution systems to resolve each other’s network locations (addresses,
protocols, and ports) from names. Peer
to peer name resolution has been complicated by transient connectivity and
shortcomings in the Domain Name system (DNS).
The Microsoft windows Peer
–to – Peer networking platform solves this problem with the Peer name
resolution protocol (PNRP).
using System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
using
System.ServiceModel;
namespace P2pWCF
{
[ServiceContract]
public interface IChat
{
[OperationContract(IsOneWay=true)]
void
SendMessage(string who, string message);
}
class ChatImp:IChat
{
public void SendMessage(string
who, string message)
{
Console.ForegroundColor
= ConsoleColor.Cyan;
Console.WriteLine("{0} Says : {1}", who, message);
Console.ForegroundColor
= ConsoleColor.Green;
}
}
}
using System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
using
System.ServiceModel;
namespace P2pWCF
{
class Program
{
static void Main(string[]
args)
{
Console.WriteLine("Enter Your name");
string
name = Console.ReadLine();
Console.WriteLine("Opening P2P Server host...");
ServiceHost
svc = new ServiceHost(typeof(ChatImp));
svc.Credentials.Peer.MeshPassword =
"paramesh@123";
svc.Open();
//Both
Server and Client binding should match
NetPeerTcpBinding
np2p = new NetPeerTcpBinding();
np2p.Security.Mode = SecurityMode.None;
EndpointAddress
ep = new EndpointAddress("net.p2p://ParamiChat");
IChat
pr = ChannelFactory<IChat>.CreateChannel(np2p, ep);
pr.SendMessage(name,string.Format("{0}
Joined the Mesh network.",name));
string
msg=string.Empty;
while(msg!="Q")
{
Console.ForegroundColor=ConsoleColor.Yellow;
msg=Console.ReadLine();
pr.SendMessage(name,msg);
}
svc.Close();
}
}
}
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<services>
<service
name="P2pWCF.ChatImp">
<endpoint
address="net.p2p://ParamiChat" binding="netPeerTcpBinding"
bindingConfiguration="NewBinding" contract="P2pWCF.IChat"></endpoint>
</service> </services>
<bindings>
<netPeerTcpBinding>
<binding
name="NewBinding">
<security
mode="None"></security>
<resolver
mode="Pnrp"></resolver>
</binding>
</netPeerTcpBinding>
</bindings>
<client>
<remove
contract="IMetadataExchanage" name="sb"/>
<endpoint
address="net.p2p://ParamiChat" binding="netPeerTcpBinding"
bindingConfiguration="NewBinding" contract="P2pWCF.IChat" name="Client"> </endpoint>
</client>
</system.serviceModel>
</configuration>