コード例 #1
0
 /// <summary>
 ///     监听消息端口
 /// </summary>
 private void ListenMessagePort()
 {
     //服务端口限制在1000以上
     if (Global.LocalPort > 1000)
     {
         try
         {
             TcpListener listener = new TcpListener(IPAddress.Any, Global.LocalPort);
             listener.Start();
             Global.TaskFactory.StartNew(() =>
             {
                 try
                 {
                     while (true)
                     {
                         Socket socket          = listener.AcceptSocket();
                         P2PTcpClient tcpClient = new P2PTcpClient(socket);
                         //接收数据
                         Global.TaskFactory.StartNew(() =>
                         {
                             Global_Func.ListenTcp <RecievePacket>(tcpClient);
                         });
                     }
                 }
                 catch (Exception ex)
                 {
                     Debug.WriteLine(ex.Message);
                 }
             });
         }
         catch
         {
             ConsoleUtils.WriteLine($"端口{Global.LocalPort}监听失败.");
         }
     }
 }