private string ClientWork(TcpClient ThisTcpClient, string SendMessage) { try { CommunicationBase CB = new CommunicationBase(ThisTcpClient); CB.SendMsg(SendMessage); return(CB.ReceiveMsg()); } catch { Debug.WriteLine("客戶端強制關閉連線!"); ThisTcpClient.Close(); ThisTcpClient.Dispose(); lock (ClientsLock) if (Clients.Contains(ThisTcpClient)) { Clients.Remove(ThisTcpClient); } } return(string.Empty); }
public void TCPClientWork() { try { while (Switch) { Console.WriteLine("等待Server訊息"); ClientEventArgs CEA = new ClientEventArgs { CommunicationBase = CB, Message = CB.ReceiveMsg() }; Console.WriteLine("收到Server訊息"); Event.ClientEventCall(CEA); } } catch { Console.WriteLine("伺服器端強制關閉連線!"); Server.Close(); } }
public void Start(string MD5String) { if (Server.Connected) { Debug.WriteLine("連線成功!"); CB = new CommunicationBase(Server); if (CB.ReceiveMsg() == MD5String) { CB.SendMsg("success"); ClientThread = new Thread(TCPClientWork) { IsBackground = true }; ClientThread.Start(); } else { CB.SendMsg("Fail"); Server.Close(); Server.Dispose(); throw new ArgumentException("MD5檢查失敗"); } } }