Exemple #1
0
 /// <summary>
 /// 实时刷新与服务器连接状态
 /// </summary>
 private void RefrshServerIsConnected()
 {
     Task.Run(() =>
     {
         while (ConnectSucceed)
         {
             if (!socketClient.Poll(500, SelectMode.SelectRead))
             {
                 ConnectSucceed = true;
             }
             else
             {
                 ConnectSucceed = false;
                 ServerShutdownEvent?.Invoke();
                 DisConnect();
             }
             Thread.Sleep(1);
         }
     });
 }
Exemple #2
0
 /// <summary>
 /// 异步接收服务器信息
 /// </summary>
 /// <param name="ar"></param>
 private void ReceiveAsync(IAsyncResult ar)
 {
     try
     {
         Socket receiveSocketAsync = ar.AsyncState as Socket;
         int    readLength         = receiveSocketAsync.EndReceive(ar);
         if (readLength > 0)
         {
             RecevierEvent?.Invoke(receiveBuffer, receiveSocketAsync.RemoteEndPoint);
             Array.Clear(receiveBuffer, 0, receiveBuffer.Length);
             receiveSocketAsync.BeginReceive(receiveBuffer, 0, receiveBuffer.Length, SocketFlags.None, ReceiveAsync, receiveSocketAsync);
         }
         else
         {
             DisConnect();
             ServerShutdownEvent?.Invoke();
         }
     }
     catch (Exception ex)
     {
         ErrInfoEvent?.Invoke(ex.Message);
     }
 }
Exemple #3
0
        /// <summary>
        /// 断开服务器连接
        /// </summary>
        public void DisConnect()
        {
            if (socketClient == null)
            {
                return;
            }

            try
            {
                socketClient.Shutdown(SocketShutdown.Both);
            }
            catch
            {
            }
            try
            {
                socketClient.Close();
            }
            catch
            {
            }
            ConnectSucceed = false;
            ServerShutdownEvent?.Invoke();
        }