程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> 關於C語言 >> c#實現的P2P網絡通訊程序(4)

c#實現的P2P網絡通訊程序(4)

編輯:關於C語言

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);
        }

3、服務端

a、消息處理線程

  private void RecvThreadProc()
        {
            IPEndPoint remotePoint = null;
            byte[] msgBuffer = null;
            while (true)
            {
                msgBuffer = server.Receive(ref remotePoint);
                try
                {
                    object msgObj = UtilityHelper.Deserialize (msgBuffer);
                    switch ((msgObj as Packet).GetCommandType())
                    {
                        case Command.MSG_USERLOGIN:         //用戶登錄
                            ProcUserLoginMsg(msgObj as UserLoginPacket, remotePoint);
                            break;
                        case Command.MSG_USERLOGOUT:        //退出登錄
                            ProcUserLogoutMsg(msgObj as UserLogoutPacket, remotePoint);
                            break;
                        case Command.MSG_GETUSERLIST:       //所有用戶列表
                            ProcGetUserListMsg(msgObj as UserListPacket, remotePoint);
                            break;
                        case Command.MSG_P2PCONNECT:        //點對點連接信息
                            ProcP2PConnectMsg(msgObj as P2PConnectionPacket, remotePoint);
                            break;
                        case Command.MSG_USERACTIVEQUERY:   // 用 戶對服務器輪詢的應答
                            ProcUserActiveQueryMsg(msgObj as UserActiveQueryPacket, remotePoint);
                            break;
                    }
                    Thread.Sleep(100);
                }
                catch { }
            }
        }

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved