P2P网络通讯程序(C#)

在网上看了很多程序(QQ、Azureus、Ants、PPStream)都实现了p2p,以前觉得技术很高深。通过这段时间的学习才发现,单纯的实现p2p在局域网通讯很容易,但是要实现外网穿透(NAT)感觉很困难。最近看了Azureus和emule源码,分别是JAVA和C++版,本人对这两门 语言都不熟悉,看起来很吃力。最后只好根据VC++实现的P2PDemo程序进行了改版,根据设计思路用c#写了一个Demo出来。通过 测试,多个客户端在局域网能脱离服务端实现端到端工作。外网的情况要通过路由器,看了Azureus要实现uPnp进行端口映射,在CodeProject上下载了一个uPnp源码看,测试结果没有启用uPnp路由器。结果现在郁闷了,不知道下一步怎么测试,是不是用upnp实现了端口自动映射成功就能实现象QQ那样通讯。

下面是程序说明:

1、公共类

公共类主要定义一些包结构

a、Packet.cs
  1. [Serializable()]
  2.     public abstract class Packet
  3.     {
  4.         /// <summary>
  5.         /// 命令类型
  6.         /// </summary>
  7.         /// <returns></returns>
  8.         public virtual int GetCommandType()
  9.         {
  10.             return -1;
  11.         }

  12.         /// <summary>
  13.         /// 用户名
  14.         /// </summary>
  15.         public string UserName
  16.         {
  17.             get;
  18.             set;
  19.         }

  20.         public Packet()
  21.         { }

  22.         public Packet(string username)
  23.         {
  24.             this.UserName = username;
  25.         }
  26.     }
复制代码
b、MassTextPacket.cs  --分片传输类
  1. [Serializable()]
  2.     public class MassTextPacket:TextPacket
  3.     {
  4.         private int seqID;
  5.         /// <summary>
  6.         /// 包序列
  7.         /// </summary>
  8.         public int SeqID
  9.         {
  10.             get { return seqID; }
  11.             set { seqID = value; }
  12.         }

  13.         private int seqCount;
  14.         /// <summary>
  15.         /// 包数量
  16.         /// </summary>
  17.         public int SeqCount
  18.         {
  19.             get { return seqCount; }
  20.             set { seqCount = value; }
  21.         }

  22.         private int _CLSD;
  23.         public int CLSD
  24.         {
  25.             get { return _CLSD; }
  26.             set { _CLSD = value; }
  27.         }

  28.     }
复制代码
2、客户端

a、消息传送时进行p2p通讯
  1.         private bool SendMessageTo(string toUserName, Packet packet)
  2.         {
  3.             PeerEntity toUser = userList.Single(c => c.UserName == toUserName);
  4.             if (toUser == null)
  5.             {
  6.                 return false;
  7.             }
  8.             ReceivedACK = false;

  9.             for (int i=0; i<MAXRETRY; i++)            
  10.             {      
  11.                 // 如果对方P2P地址不为0,就试图以它为目的地址发送数据,
  12.                 // 如果发送失败,则认为此P2P地址无效
  13.                 if (toUser.P2PAddress != null && toUser.P2PAddress.Port != 0)
  14.                 {
  15.                     if (packet.GetType() == typeof(TextPacket))
  16.                     {
  17.                         TextPacket msgPacket = new TextPacket(toUserName, (packet as TextPacket).Message);
  18.                         byte[] buffer = UtilityHelper.Serialize(msgPacket);
  19.                         if (buffer.Length > MAXBUFFERSIZE)
  20.                         {
  21.                         
  22.                             MassTextPacket mtp = new MassTextPacket();
  23.                             mtp.SeqID = 0;
  24.                             mtp.SeqCount = (int)System.Math.Ceiling(buffer.Length / (decimal)MAXBUFFERSIZE);
  25.                             mtp.CLSD = mtp.GetHashCode();
  26.                             
  27.                             long pos = 0;
  28.                             long count = buffer.Length < MAXBUFFERSIZE ? buffer.Length : MAXBUFFERSIZE;
  29.                             while (pos < buffer.Length && pos > 0)
  30.                             {
  31.                                 byte[] bytes = new byte[count];                          ;
  32.                                 for (int k = 0; k < count; k++)
  33.                                     bytes[k] = buffer[pos + k];
  34.                                 //数据组包
  35.                                 mtp.SeqID = mtp.SeqID + 1;
  36.                                 mtp.Message = Convert.ToBase64String(bytes);

  37.                                 //发送数据
  38.                                 byte[] buf = UtilityHelper.Serialize(mtp);
  39.                                 client.Send(buf, buf.Length, toUser.P2PAddress);
  40.                                 Thread.Sleep(100);
  41.                             }
  42.                         }
  43.                         else
  44.                             client.Send(buffer, buffer.Length, toUser.P2PAddress);
  45.                     }
  46.                     else if (packet.GetType() == typeof(FileStreamPacket))
  47.                     {
  48.                         FileStreamPacket fsp = packet as FileStreamPacket;
  49.                         System.IO.FileStream fs = new System.IO.FileStream(fsp.FileName, System.IO.FileMode.Open, System.IO.FileAccess.Read, FileShare.Read);

  50.                       handle1.Reset();

  51.                         fsp.SeqID = 0;
  52.                         fsp.SeqCount = (int)System.Math.Ceiling(fs.Length / (decimal)MAXBUFFERSIZE);
  53.                         fsp.CLSD = fsp.GetHashCode();

  54.                         long pos = 0;
  55.                         long count = fs.Length < MAXBUFFERSIZE ? fs.Length : MAXBUFFERSIZE;
  56.                         while (pos < fs.Length && count > 0)
  57.                         {
  58.                             byte[] buffer = new byte[count];
  59.                             fs.Seek(pos, SeekOrigin.Begin);
  60.                             fs.Read(buffer, 0, (int)count);

  61.                             pos += count;
  62.                             count = pos + MAXBUFFERSIZE < fs.Length ? MAXBUFFERSIZE : fs.Length - pos;

  63.                             //数据组包
  64.                             fsp.SeqID = fsp.SeqID + 1;
  65.                             fsp.Message = Convert.ToBase64String(buffer);

  66.                             //发送数据
  67.                             byte[] buf = UtilityHelper.Serialize(fsp);
  68.                             client.Send(buf, buf.Length, toUser.P2PAddress);
  69.                             Thread.Sleep(300);
  70.                         }

  71.                         handle1.Set();
  72.                     }

  73.                     // 等待接收线程将标记修改
  74.                     for (int j = 0; j < 10; j++)
  75.                     {
  76.                         if (this.ReceivedACK)
  77.                         {
  78.                             this.ReceivedACK = false;
  79.                             return true;
  80.                         }
  81.                         else
  82.                         {
  83.                             Thread.Sleep(300);
  84.                         }
  85.                     }
  86.                 }

  87.                 // 构建P2P打洞封包
  88.                 // 然后通过服务器转发,请求对方向自己打洞
  89.                 P2PConnectionPacket transMsg = new P2PConnectionPacket(UserName, toUserName);
  90.                 byte[] msgBuffer = UtilityHelper.Serialize(transMsg);
  91.                 client.Send(msgBuffer, msgBuffer.Length, hostPoint);

  92.                 // 等待对方的P2PCONNECTACK消息
  93.                 for(int j = 0; j < 10; ++j)
  94.                 {
  95.                     toUser = userList.Single(c => c.UserName == toUserName);
  96.                     if ( toUser.P2PAddress != null && toUser.P2PAddress.Port != 0)
  97.                         break;
  98.                     Thread.Sleep(300);
  99.                 }
  100.                 
  101.             }
  102.             return false;
  103.         }
复制代码
b、消息接受线程
  1. /// <summary>
  2.         /// 接受线程处理
  3.         /// </summary>
  4.         private void RecvThreadProc()
  5.         {
  6.             byte[] buffer;
  7.             while (true)
  8.             {
  9.                 
  10.                 
  11.                 buffer = client.Receive(ref remotePoint);
  12.                 Packet packet = UtilityHelper.Deserialize(buffer) as Packet;      
  13.                 Type msgType = packet.GetType();
  14.                 if (msgType == typeof(UserListAckPacket))
  15.                 {
  16.                     // 转换消息
  17.                     UserListAckPacket usersMsg = (UserListAckPacket)packet;
  18.                     // 更新用户列表
  19.                     userList.Clear();
  20.                     foreach (PeerEntity user in usersMsg.Users)
  21.                     {
  22.                         userList.Add(user);
  23.                     }
  24.                     bUserListComplete = true;
  25.                 }                
  26.                 else if (msgType == typeof(UserLoginAckPacket))
  27.                 {
  28.                     ProcUserLogAckMsg(packet);
  29.                 }        
  30.                 else if (msgType == typeof(TextPacket))
  31.                 {
  32.                     // 转换消息
  33.                     TextPacket txtPacket = (TextPacket)packet;
  34.                     printf("Receive a message: {0}", txtPacket.Message);
  35.                     // 发送应答消息
  36.                     P2PAckPacket ackMsg = new P2PAckPacket();
  37.                     buffer = UtilityHelper.Serialize(ackMsg);
  38.                     client.Send(buffer, buffer.Length, remotePoint);
  39.                 }
  40.                 else if (msgType == typeof(MassTextPacket))
  41.                 {
  42.                     lock (this)
  43.                     {
  44.                         MassTextPacket fPacket = (MassTextPacket)packet;
  45.                         if (packets.ContainsKey(fPacket.CLSD))
  46.                             packets[fPacket.CLSD].Add(fPacket);
  47.                         else
  48.                             packets.Add(fPacket.CLSD, new List<MassTextPacket>() { fPacket });

  49.                         printf("PacketID:{0}--SeqNo:{1}--progress:{2}%", fPacket.CLSD, fPacket.SeqID, (int)(System.Math.Round(packets[fPacket.CLSD].Count / (decimal)(fPacket as MassTextPacket).SeqCount, 2) * 100));
  50.                         //组包
  51.                         if ((fPacket as MassTextPacket).SeqCount == packets[fPacket.CLSD].Count)
  52.                         {
  53.                             List<MassTextPacket> temp = packets[fPacket.CLSD].OrderBy(c => c.SeqID).ToList();
  54.                             List<byte> values = new List<byte>();
  55.                             foreach (MassTextPacket mt in temp)
  56.                             {
  57.                                 byte[] buf = Convert.FromBase64String(mt.Message);        
  58.                                 values.AddRange(buf);
  59.                             }

  60.                             MassTextPacket value = UtilityHelper.Deserialize(values.ToArray()) as MassTextPacket;

  61.                             printf("Receive a message: {0}", value.Message);

  62.                           
  63.                             // 发送应答消息
  64.                             P2PAckPacket ackMsg = new P2PAckPacket();
  65.                             buffer = UtilityHelper.Serialize(ackMsg);
  66.                             client.Send(buffer, buffer.Length, remotePoint);
  67.                         }
  68.                     }

  69.                 }
  70.                 else if (msgType == typeof(FileStreamPacket))
  71.                 {                
  72.                     lock (this)                    
  73.                     {
  74.                         FileStreamPacket fPacket = (FileStreamPacket)packet;
  75.                         if (packets.ContainsKey(fPacket.CLSD))
  76.                             packets[fPacket.CLSD].Add(fPacket);
  77.                         else
  78.                             packets.Add(fPacket.CLSD, new List<MassTextPacket>() { fPacket });

  79.                         printf("PacketID:{0}--SeqNo:{1}--progress:{2}%", fPacket.CLSD, fPacket.SeqID, (int)(System.Math.Round(packets[fPacket.CLSD].Count / (decimal)(fPacket as FileStreamPacket).SeqCount, 2) * 100));
  80.                         //组包
  81.                         if ((fPacket as FileStreamPacket).SeqCount == packets[fPacket.CLSD].Count)
  82.                         {
  83.                             List<MassTextPacket> temp = packets[fPacket.CLSD].OrderBy(c => c.SeqID).ToList();

  84.                             System.IO.FileStream fs = new System.IO.FileStream((fPacket as FileStreamPacket).FileName + ".tmp", System.IO.FileMode.Create, System.IO.FileAccess.ReadWrite);
  85.                             foreach (FileStreamPacket mt in temp)
  86.                             {
  87.                                 byte[] buf = Convert.FromBase64String(mt.Message);
  88.                                 fs.Write(buf, 0, buf.Length);

  89.                             }
  90.                             fs.Flush();
  91.                             fs.Close();
  92.                           printf("Receive a file: {0}", (fPacket as FileStreamPacket).FileName);

  93.                           //清除数据包
  94.                           packets[fPacket.CLSD].Clear();

  95.                           // 发送应答消息
  96.                           P2PAckPacket ackMsg = new P2PAckPacket();
  97.                           buffer = UtilityHelper.Serialize(ackMsg);
  98.                           client.Send(buffer, buffer.Length, remotePoint);
  99.                         }
  100.                     }              
  101.                   
  102.                 }
  103.                 else if (msgType == typeof(P2PAckPacket))
  104.                 {
  105.                     this.ReceivedACK = true;
  106.                 }
  107.                 else if (msgType == typeof(P2PPurchHolePacket))
  108.                 {
  109.                     ProcP2PPurchHoleMsg(packet, remotePoint);
  110.                 }
  111.                 else if (msgType == typeof(P2PPurchHoleAckPacket))
  112.                 {
  113.                     PeerEntity touser = userList.SingleOrDefault(c => c.UserName == (packet as P2PPurchHoleAckPacket).ToUserName);
  114.                     //更改本地的P2P连接时使用的IP地址
  115.                     touser.P2PAddress = touser.RemoteEndPoint;
  116.                 }
  117.                 Thread.Sleep(100);
  118.             }

  119.         }
复制代码
c.建立p2p会话
  1.         private void ProcP2PPurchHoleMsg(Packet packet,IPEndPoint remoteEP)
  2.         {
  3.             //打洞请求消息          
  4.             P2PPurchHolePacket purchReqMsg = (P2PPurchHolePacket)packet;
  5.             PeerEntity toUser = userList.Single(c => c.UserName == purchReqMsg.ToUserName);
  6.             PeerEntity user = userList.Single(c => c.UserName == purchReqMsg.UserName);
  7.             toUser.P2PAddress = toUser.RemoteEndPoint;
  8.             printf("Set P2P Address for {0}->[{1}]", user.UserName, toUser.P2PAddress.ToString());  
  9.         
  10.             //uPnp实现端口映射
  11.             if(NAT.AddPortMapping(toUser.P2PAddress.Port, ProtocolType.Udp, "AddPortMapping"))
  12.                 printf("Port mapping successed!");

  13.             // 发送打洞消息到远程主机
  14.             P2PPurchHoleAckPacket trashMsg = new P2PPurchHoleAckPacket(purchReqMsg.UserName, purchReqMsg.ToUserName);          
  15.             byte[] buffer = UtilityHelper.Serialize(trashMsg);
  16.             client.Send(buffer, buffer.Length, user.RemoteEndPoint);
  17.         }
复制代码
3、服务端

a、消息处理线程
  1.   private void RecvThreadProc()
  2.         {
  3.             IPEndPoint remotePoint = null;
  4.             byte[] msgBuffer = null;
  5.             while (true)
  6.             {            
  7.                 msgBuffer = server.Receive(ref remotePoint);
  8.                 try
  9.                 {
  10.                     object msgObj = UtilityHelper.Deserialize(msgBuffer);
  11.                     switch ((msgObj as Packet).GetCommandType())
  12.                     {
  13.                         case Command.MSG_USERLOGIN:        //用户登录                            
  14.                             ProcUserLoginMsg(msgObj as UserLoginPacket, remotePoint);
  15.                             break;
  16.                         case Command.MSG_USERLOGOUT:        //退出登录
  17.                             ProcUserLogoutMsg(msgObj as UserLogoutPacket, remotePoint);
  18.                             break;
  19.                         case Command.MSG_GETUSERLIST:      //所有用户列表                            
  20.                             ProcGetUserListMsg(msgObj as UserListPacket, remotePoint);
  21.                             break;
  22.                         case Command.MSG_P2PCONNECT:        //点对点连接信息                          
  23.                             ProcP2PConnectMsg(msgObj as P2PConnectionPacket, remotePoint);
  24.                             break;
  25.                         case Command.MSG_USERACTIVEQUERY:  // 用户对服务器轮询的应答                            
  26.                             ProcUserActiveQueryMsg(msgObj as UserActiveQueryPacket, remotePoint);
  27.                             break;
  28.                     }
  29.                     Thread.Sleep(100);
  30.                 }
  31.                 catch { }
  32.             }
  33.         }
复制代码
b、服务端请求客户端建立p2p连接
  1.         private void ProcP2PConnectMsg(Packet packet,IPEndPoint remoteEP)
  2.         {
  3.             // 转换接受的消息
  4.             P2PConnectionPacket transMsg = (P2PConnectionPacket)packet;
  5.             printf("{0}({1}) wants to p2p {2}", remoteEP.Address.ToString(), transMsg.UserName, transMsg.ToUserName);
  6.             // 获取目标用户
  7.             PeerEntity toUser = userList.SingleOrDefault(c => c.UserName == transMsg.ToUserName);
  8.             
  9.             // 转发Purch Hole请求消息
  10.             P2PPurchHolePacket transMsg2 = new P2PPurchHolePacket(transMsg.UserName, toUser.UserName);
  11.             //转发消息
  12.             byte[] buffer = UtilityHelper.Serialize(transMsg2);
  13.             server.Send(buffer, buffer.Length, toUser.RemoteEndPoint);
  14.           
  15.         }
复制代码
4、测试

a、服务端

附件:  p2p_srv.jpg 

b、客户端

附件:  p2p_b.jpg 

附件:  p2p_a.jpg 

困惑:

1、能不能实现外网通讯,要实现像QQ那样通讯要做哪些改进。

2、文件续传如何实现。

3、c#封装的网络操作类(像QQ.NET源码的Net实现)

4、远程协助的实现。

DEMO下载: [ 评价与收藏 ]
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是一个简单的C# P2P程序代码示例,实现了基于UDP协议的P2P通信,可以相互发送文本消息: ```csharp using System; using System.Net; using System.Net.Sockets; using System.Text; class Program { static void Main(string[] args) { Console.Write("请输入本地IP地址:"); string localIP = Console.ReadLine(); Console.Write("请输入本地端口号:"); int localPort = int.Parse(Console.ReadLine()); Console.Write("请输入远程IP地址:"); string remoteIP = Console.ReadLine(); Console.Write("请输入远程端口号:"); int remotePort = int.Parse(Console.ReadLine()); UdpClient udpClient = new UdpClient(localPort); udpClient.JoinMulticastGroup(IPAddress.Parse(remoteIP)); IPEndPoint remoteEP = new IPEndPoint(IPAddress.Parse(remoteIP), remotePort); IPEndPoint localEP = new IPEndPoint(IPAddress.Parse(localIP), localPort); Console.WriteLine("P2P聊天程序已启动,输入quit退出程序"); while (true) { Console.Write("请输入要发送的消息:"); string message = Console.ReadLine(); if (message == "quit") { break; } byte[] data = Encoding.UTF8.GetBytes(message); udpClient.Send(data, data.Length, remoteEP); Console.WriteLine("已发送消息:{0}", message); data = udpClient.Receive(ref localEP); message = Encoding.UTF8.GetString(data); Console.WriteLine("收到消息:{0}", message); } udpClient.Close(); Console.WriteLine("程序已退出"); Console.ReadKey(); } } ``` 注意,这个示例代码只是一个简单的P2P程序示例,实际使用时需要加入更多的安全性、可靠性和稳定性措施。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值