WEB开发网
开发学院软件开发C语言 P2P网络通讯程序(c#) 阅读

P2P网络通讯程序(c#)

 2009-03-27 08:22:11 来源:WEB开发网   
核心提示: c.建立p2p会话 private void ProcP2PPurchHoleMsg(Packet packet,IPEndPoint remoteEP) { //打洞请求消息 P2PPurchHolePacket purchReqMsg = (P2PPurchHolePacket)pa

c.建立p2p会话

        private void ProcP2PPurchHoleMsg(Packet packet,IPEndPoint remoteEP)
        {
            //打洞请求消息          
            P2PPurchHolePacket purchReqMsg = (P2PPurchHolePacket)packet;
            PeerEntity toUser = userList.Single(c => c.UserName == purchReqMsg.ToUserName);
            PeerEntity user = userList.Single(c => c.UserName == purchReqMsg.UserName);
            toUser.P2PAddress = toUser.RemoteEndPoint;
            printf("Set P2P Address for {0}->[{1}]", user.UserName, toUser.P2PAddress.ToString());  
        
            //uPnp实现端口映射
            if(NAT.AddPortMapping(toUser.P2PAddress.Port, ProtocolType.Udp, "AddPortMapping"))
                printf("Port mapping successed!");

            // 发送打洞消息到远程主机
            P2PPurchHoleAckPacket trashMsg = new P2PPurchHoleAckPacket(purchReqMsg.UserName, purchReqMsg.ToUserName);         
            byte[] buffer = UtilityHelper.Serialize(trashMsg);
            client.Send(buffer, buffer.Length, user.RemoteEndPoint);
        }

上一页  1 2 3 4 5 6  下一页

Tags:PP 网络通讯 程序

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接