关于互联网和c#上的p2p连接。阿盖恩
这个主题类似于和。我也读过 但是,在这篇文章中,我找不到清晰的例子来说明如何在互联网上组织p2p连接。 所有示例在本地网络中都能很好地工作 我做了一个小程序,你可以在下面看到。它在本地网络中运行良好 主要问题:对等注册后,如何打开Wcf主机并等待来自其他对等方的呼叫?我在书中读到: 在全局云上解析对等方时,通常会将其解析为IPv6地址 所以我需要在这个IPv6地址上打开我的WCF?我怎样才能收到它? 请给我一些编码技巧 我的节目:关于互联网和c#上的p2p连接。阿盖恩,c#,wcf,p2p,C#,Wcf,P2p,这个主题类似于和。我也读过 但是,在这篇文章中,我找不到清晰的例子来说明如何在互联网上组织p2p连接。 所有示例在本地网络中都能很好地工作 我做了一个小程序,你可以在下面看到。它在本地网络中运行良好 主要问题:对等注册后,如何打开Wcf主机并等待来自其他对等方的呼叫?我在书中读到: 在全局云上解析对等方时,通常会将其解析为IPv6地址 所以我需要在这个IPv6地址上打开我的WCF?我怎样才能收到它? 请给我一些编码技巧 我的节目: namespace FileShare { intern
namespace FileShare
{
internal class Program
{
private static void Main()
{
Console.WriteLine("Hello enter your username");
IPeerRegistrationRepository peerRegistrationRepository = new PeerRegistrationRepository();
IPeerNameResolverRepository peerNameResolverRepository = new PeerNameResolverRepository();
ServiceHost host = null;
try
{
string username = Console.ReadLine();
int port = PeerUtils.FindFreePort();
peerRegistrationRepository.StartRegistration("Max951Peer", port, PeerNameType.Secured);
string serviceUrl = $"net.tcp://{peerRegistrationRepository.PeerName.PeerHostName}:{port}/Max951P2PService";
IP2PService localService = new P2PService();
host = new ServiceHost(localService, new Uri(serviceUrl));
host.AddServiceEndpoint(typeof(IP2PService), new NetTcpBinding { Security = { Mode = SecurityMode.None } }, serviceUrl);
try
{
host.Open();
Console.WriteLine($"Host opened on { serviceUrl }");
}
catch (Exception e)
{
Console.WriteLine($"Error { e }");
return;
}
Console.WriteLine($"Registered peer. PeerHostName: { peerRegistrationRepository.PeerName.PeerHostName }, Classifier: { peerRegistrationRepository.PeerName.Classifier }");
PeerNameRecordCollection results = peerNameResolverRepository.ResolvePeerName(peerRegistrationRepository.PeerName, Cloud.Global);
Console.WriteLine("{0} Peers Found:", results.Count.ToString());
int i = 1;
foreach (PeerNameRecord peer in results)
{
Console.WriteLine("{0} Peer:{1}", i++, peer.PeerName);
foreach (IPEndPoint ip in peer.EndPointCollection)
{
Console.WriteLine("\t Endpoint: {0}", ip);
string endpointUrl = $"net.tcp://[{ip.Address}]:{ip.Port}/Max951P2PService";
NetTcpBinding binding = new NetTcpBinding { Security = { Mode = SecurityMode.None } };
try
{
IP2PService serviceProxy = ChannelFactory<IP2PService>.CreateChannel(binding, new EndpointAddress(endpointUrl));
serviceProxy.SendMessage("Hello!", username);
}
catch (Exception)
{
// ignored
}
}
}
Console.ReadLine();
}
catch (Exception e)
{
Console.WriteLine(e);
}
finally
{
peerRegistrationRepository.StopRegistration();
host?.Close();
}
//serviceModelClient.Stop();
}
}
[ServiceContract]
public interface IP2PService
{
[OperationContract(IsOneWay = true)]
void SendMessage(string message, string from);
}
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
public class P2PService : IP2PService
{
/// <inheritdoc />
public void SendMessage(string message, string from)
{
Console.WriteLine($"{from} says: { message }");
}
}
}
namespace Utils.PeerToPeer
{
public class PeerNameResolverRepository : IPeerNameResolverRepository
{
private readonly PeerNameResolver peerNameResolver;
public PeerNameResolverRepository()
{
this.peerNameResolver = new PeerNameResolver();
}
/// <inheritdoc />
public PeerNameRecordCollection ResolvePeerName(string name, PeerNameType peerNameType, Cloud cloud)
{
if (string.IsNullOrEmpty(name))
{
throw new ArgumentNullException(nameof(name));
}
return this.peerNameResolver.Resolve(new PeerName(name, peerNameType), cloud);
}
/// <inheritdoc />
public PeerNameRecordCollection ResolvePeerName(PeerName peerName, Cloud cloud)
{
return this.peerNameResolver.Resolve(peerName, cloud);
}
}
}
namespace Utils.PeerToPeer
{
public class PeerRegistrationRepository : IPeerRegistrationRepository
{
private PeerNameRegistration peerNameRegistration;
/// <inheritdoc />
public bool IsRegistered => this.peerNameRegistration != null && this.peerNameRegistration.IsRegistered();
/// <inheritdoc />
public string PeerUri => GetPeerUri();
/// <inheritdoc />
public PeerName PeerName { get; set; }
/// <inheritdoc />
public void StartRegistration(string name, int port, PeerNameType peerNameType)
{
this.PeerName = new PeerName(name, peerNameType);
this.peerNameRegistration = new PeerNameRegistration(PeerName, port, Cloud.Global);
this.peerNameRegistration.Start();
}
/// <inheritdoc />
public void StopRegistration()
{
this.peerNameRegistration?.Stop();
this.peerNameRegistration = null;
}
private string GetPeerUri()
{
return this.peerNameRegistration?.PeerName.PeerHostName;
}
}
}
名称空间文件共享
{
内部课程计划
{
私有静态void Main()
{
Console.WriteLine(“您好,请输入您的用户名”);
IPeerRegistrationRepository peerRegistrationRepository=新的peerRegistrationRepository();
IPeerNameResolveRestory PeerNameResolveRestory=新PeerNameResolveRestory();
ServiceHost主机=null;
尝试
{
字符串username=Console.ReadLine();
int port=PeerUtils.FindFreePort();
peerRegistrationRepository.StartRegistration(“Max951Peer”,端口,PeerNameType.Secured);
字符串serviceUrl=$“net.tcp://{peerRegistrationRepository.PeerName.PeerHostName}:{port}/Max951P2PService”;
IP2PService localService=新的P2PService();
主机=新服务主机(localService,新Uri(serviceUrl));
AddServiceEndpoint(typeof(IP2PService),新的NetTcpBinding{Security={Mode=SecurityMode.None},serviceUrl);
尝试
{
host.Open();
WriteLine($“主机在{serviceUrl}上打开”);
}
捕获(例外e)
{
WriteLine($“Error{e}”);
返回;
}
WriteLine($“已注册的peer.PeerHostName:{peerRegistrationRepository.PeerName.PeerHostName},分类器:{peerRegistrationRepository.PeerName.Classifier}”);
PeerNameRecordCollection结果=PeerNameResolverRecository.ResolvePeerName(peerRegistrationRepository.PeerName,Cloud.Global);
WriteLine(“{0}发现:”,results.Count.ToString());
int i=1;
foreach(PeerNameRecord对等结果)
{
WriteLine(“{0}Peer:{1}”,i++,Peer.PeerName);
foreach(peer.endpoint集合中的IPEndPoint ip)
{
WriteLine(“\t端点:{0}”,ip);
字符串endpointUrl=$“net.tcp://[{ip.Address}]:{ip.Port}/Max951P2PService”;
NetTcpBinding=new NetTcpBinding{Security={Mode=SecurityMode.None};
尝试
{
IP2PService serviceProxy=ChannelFactory.CreateChannel(绑定,新端点地址(端点URL));
SendMessage(“你好!”,用户名);
}
捕获(例外)
{
//忽略
}
}
}
Console.ReadLine();
}
捕获(例外e)
{
控制台写入线(e);
}
最后
{
peerRegistrationRepository.StopRegistration();
主机?.Close();
}
//serviceModelClient.Stop();
}
}
[服务合同]
公共接口IP2PService
{
[运营合同(IsOneWay=true)]
void SendMessage(字符串消息,字符串来自);
}
[ServiceBehavior(InstanceContextMode=InstanceContextMode.Single)]
公共类P2P服务:IP2PService
{
///
public void SendMessage(字符串消息、字符串发件人)
{
WriteLine($“{from}表示:{message}”);
}
}
}
命名空间Utils.PeerToPeer
{
公共类PeerNameResolveRestory:IperNameResolveRestory
{
专用只读PeerNameResolver PeerNameResolver;
公共peernamesolverrepository()
{
this.peerNameResolver=新的peerNameResolver();
}
///
公共PeerNameRecordCollection解析peerName(字符串名称、PeerNameType、PeerNameType、云)
{
if(string.IsNullOrEmpty(name))
{
抛出新ArgumentNullException(nameof(name));
}
返回这个.peerNameResolver.Resolve(新的PeerName(名称,peerNameType),cloud);
}
///
公共PeerNameRecordCollection解析PeerName(PeerName PeerName,Cloud)
{
返回这个.peernamesolver.Resolve(peerName,cloud);
}
}
}
命名空间Utils.PeerToPeer
{
公共类对等注册存储库:IPeerRegistrationRepository
{
私人对等注册对等注册;
///
public bool IsRegistered=>this.peerNameRegistration!=null&&this.peerNameRegistration.IsRegistered();
///
公共字符串PeerUri=>GetPeerUri();
///
公共对等名称对等名称{get;set;}
///
public void StartRegistration(字符串名称、int端口、PeerNameType PeerNameType)
{
this.PeerName=新的PeerName(名称,peerNameType);
this.peerNameRegistration=新的peerNameRegistration(PeerName、port、Cloud.Global);
this.peerNameRegistration.Start();
}
///
公开注册()
{
这个.peerNameRegistration?.Stop();