C# p2p(peer2peer)通信(基于PNRP协议)
PNRP Peer name Resolution Protocol对等名称解析协议,是由微软公司设计的基于IPv4和IPv6的点对点协议。PNRP云,这类云已被弃用,尽管它仍受 PNRP 支持。 云 PNRP 云由 Cloud 类的实例表示。 对等机使用的多组云由可枚举的 CloudCollection 类的实例表示。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Net.PeerToPeer;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace P2PTest230508
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.Load += Form1_Load;
button_regjster.Click += Button_regjster_Click;
button_remove.Click += Button_remove_Click;
button_search.Click += Button_search_Click;
listView_result.View = View.Details;
listView_result.Columns.Add("图标",10);
listView_result.Columns.Add("位置",200);
listView_result.Columns.Add("发布时间",200);
}
private void Button_search_Click(object sender, EventArgs e)
{
if (string.IsNullOrWhiteSpace(textBox_searchName.Text))
{
return;
}
listView_result.Items.Clear();
PeerName peerName = new PeerName(textBox_searchName.Text, PeerNameType.Unsecured);
PeerNameResolver peerNameResolver = new PeerNameResolver();
PeerNameRecordCollection peerNameRecords = peerNameResolver.Resolve(peerName);
foreach (var record in peerNameRecords)
{
foreach (var ipe in record.EndPointCollection)
{
if (ipe.AddressFamily==AddressFamily.InterNetwork)
{
ListViewItem item = new ListViewItem();
item.SubItems.Add(ipe.ToString());
item.SubItems.Add(Encoding.UTF8.GetString(record.Data));
listView_result.Items.Add(item);
}
}
}
}
private void Button_remove_Click(object sender, EventArgs e)
{
if (count==0)
{
return;
}
int index = listBox_share.SelectedIndex;
if (index<0)
{
return;
}
for (int i = 0; i < count; i++)
{
if (string.Equals(peerNameRegistrations[i].Comment,listBox_share.Items[index].ToString()))
{
peerNameRegistrations[i].Stop();
listBox_share.Items.RemoveAt(index);
break;
}
}
}
private void Button_regjster_Click(object sender, EventArgs e)
{
if (string.IsNullOrWhiteSpace(textBox_resourceName.Text))
{
return;
}
//创建对等名
PeerName peerName = new PeerName(textBox_resourceName.Text,PeerNameType.Unsecured);
for (int i = 0; i < count; i++)
{
if (string.Equals(peerNameRegistrations[i].Comment, peerName.ToString()))
{
MessageBox.Show($"{ textBox_resourceName.Text} 对等名已存在,不能重复注册");
return;
}
}
//注册对等名
peerNameRegistrations[count] = new PeerNameRegistration(peerName,Convert.ToInt32(textBox_localPort.Text));
peerNameRegistrations[count].Comment = peerName.ToString();
peerNameRegistrations[count].Data = Encoding.UTF8.GetBytes(DateTime.Now.ToString());
peerNameRegistrations[count].Cloud = Cloud.Global;
peerNameRegistrations[count].Start();
count++;
listBox_share.Items.Add(peerName.ToString());
}
private void Form1_Load(object sender, EventArgs e)
{
IPAddress ip = Dns.GetHostEntry(Dns.GetHostName()).AddressList[0];
textBox_localIp.Text = ip.MapToIPv4().ToString();
int port = new Random().Next(10000, 50000);
textBox_localPort.Text = port.ToString();
}
int count = 0;
PeerNameRegistration[] peerNameRegistrations=new PeerNameRegistration[50];
}
}