1、事件源对象e事件实体public delegate void UdpUserLogInDelegate(object sender,UDPSockEventArgs e); / / 一般UDP消息事件委托 public delegate void UdpMessageDelegate(object sender,UDPSockEventArgs e); / 初始化一个新连接的事件委托 public delegate void UdpNewConnectDelegate(object sender,UDPSockEventArgs e);/ P2P共享数据类public class P2PCon
2、sts/ UDP服务器监听端口public const int UDP_SRV_PORT = 2280;/TCP服务器监听端口public const int TCP_SRV_PORT =2000; / FormatterHelper 序列化,反序列化消息的帮助类public class FormatterHelperpublic static byte Serialize(object obj)BinaryFormatter binaryF = new BinaryFormatter();MemoryStream ms = new MemoryStream(1024*10);binaryF.
3、Serialize(ms, obj);ms.Seek(0, SeekOrigin.Begin);byte buffer = new byte(int)ms.Length;ms.Read(buffer, 0, buffer.Length);ms.Close();return buffer;public static object Deserialize(byte buffer)MemoryStream ms = new MemoryStream(buffer, 0, buffer.Length, false);object obj = binaryF.Deserialize(ms);return
4、 obj;/ 用于承载UDPSock信息的事件类public class UDPSockEventArgs:EventArgs/ 要承载的消息private string m_strMsg;/ 用户信息private string m_strUserName; / 触发该事件的公共终端 private IPEndPoint m_EndPoint;/ 初始化UDPSock事件sMsg用户发送的信息public UDPSockEventArgs(string sMsg):base()this.m_strMsg =sMsg;/ 远端用户名public string RemoteUserNameget
5、return m_strUserName;setm_strUserName=value; / 一般套接字消息 public string SockMessage get return m_strMsg; set m_strMsg = value; / 公共远端节点 public IPEndPoint RemoteEndPoint return m_EndPoint; m_EndPoint = value;/UDPP2PSock.csusing System.Collections.Generic;using System.Text;using System.Net;using System.N
6、et.Sockets;using System.Threading;using P2PWellKnown;namespace UDPP2P / UDPP2P套接字管理类 public class UDPP2PSock / 用户登录事件 public event UdpUserLogInDelegate OnUserLogInU; / 一般UDP消息事件 public event UdpMessageDelegate OnSockMessageU; / 初始化一个新连接事件 public event UdpNewConnectDelegate OnNewConnectU; / UDP服务器 pr
7、ivate UdpClient m_udpServer; / UDP客户端 private UdpClient m_udpClient; / 服务器实际上在本地机器上监听的 / 端口,用于当一台计算机上同时启 / 动两个可两以上服务器进程时,标 / 识不同的服务器进程 private int m_iMyServerPort; / 客户端在本地机器上实际使用的端口, / 用于当一台计算机上同时有两个或两 / 个以上客户端进程在运行时,标识不 / 同的客户端进程 private int m_iMyClientPort; / 标识是否已成功创服务器 private bool m_bServerCre
8、ated; / 标识是否已成功创建客户端 private bool m_bClientCreated; / 服务器使用的线程 private Thread m_serverThread; / 客户端使用的线程 private Thread m_clientThread; / 打洞线程 /private Thread m_burrowThread; / 远端节点 private IPEndPoint m_remotePoint; / 当前进程作为客户端的公共终端 private string m_strMyPublicEndPoint; / 当前进程作为客户端的私有终端 private stri
9、ng m_strMyPrivateEndPoint; / 用于接受信息的StringBuilder实例 private StringBuilder m_sbResponse = new StringBuilder(); / P2P打洞时标识是否收到回应消息 private bool m_bRecvAck=false ; / 请求向其方向打洞的私有终端 private IPEndPoint m_requestPrivateEndPoint; / 请求向其方向打洞的公共终端 private IPEndPoint m_requestPublicEndPoint; / 打洞消息要发向的节点 priva
10、te ToEndPoint m_toEndPoint; / 用于标识是否已经和请求客户端建立点对连接 /private bool m_bHasConnected=false ; / 创建服务器或客户端的最大尝试 / 次数,为(65536-60000),防止 / 因不能创建而限入死循环或使用 / 无效端口 private const int MAX_CREATE_TRY = 5536; / 打洞时尝试连接的最大尝试次数 private const int MAX_CONNECT_TRY = 10; / 构造函数,初始化UDPP2P实例 public UDPP2PSock() m_iMyServe
11、rPort = P2PConsts.UDP_SRV_PORT; m_iMyClientPort = 60000; m_bClientCreated = false; m_bServerCreated = false; m_toEndPoint = new ToEndPoint(); m_serverThread = new Thread(new ThreadStart(RunUDPServer ); m_clientThread = new Thread(new ThreadStart(RunUDPClient ); /m_burrowThread = new Thread(new Threa
12、dStart(BurrowProc); / 创建UDP服务器 public void CreateUDPSever() int iTryNum=0; /开始尝试创建服务器 while (!m_bServerCreated & iTryNum MAX_CREATE_TRY) try m_udpServer = new UdpClient(m_iMyServerPort); m_bServerCreated = true; catch m_iMyServerPort+; iTryNum+; /创建失败,抛出异常 if (! iTryNum = MAX_CREATE_TRY) throw new E
13、xception (创建服务器尝试失败!); m_serverThread.Start(); / 创建UDP客户端strServerIP服务器IPiServerPort服务器端口 public void CreateUDPClient(string strServerIP,int iServerPort) int iTryNum = 0;m_bClientCreated & m_udpClient = new UdpClient(m_iMyClientPort ); m_bClientCreated = true; string strIPAddress = (System.Net.Dns.G
14、etHostAddresses(localhost)0).ToString(); m_strMyPrivateEndPoint = strIPAddress + : + m_iMyClientPort.ToString(); m_iMyClientPort +;创建客户端尝试失败! IPEndPoint hostPoint = new IPEndPoint(IPAddress.Parse(strServerIP), iServerPort); string strLocalIP = (System.Net.Dns.GetHostAddresses()0.ToString(); SendLoca
15、lPoint(strLocalIP, m_iMyClientPort, hostPoint); m_clientThread .Start(); / 运行UDP服务器 private void RunUDPServer() while (true) byte msgBuffer =m_udpServer .Receive(ref m_remotePoint); m_sbResponse.Append(System.Text.Encoding.Default.GetString(msgBuffer); CheckCommand(); Thread.Sleep(10); / 运行UDP客户端 private void RunUDPClient()
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1