private void ReceiveMessage() { while (true) { try { int length = client.Receive(result); if (length == 0) // 这里 Unity关闭后 会一直接受到Unity客户端的空包 未找到原因 { continue; } DeleEvent.sendMessage -= SendMessage; DeleEvent.sendMessage(clientID + ":" + Encoding.UTF8.GetString(result, 0, length)); DeleEvent.sendMessage += SendMessage; //SendMessage("999:我知道了,你可以闭嘴了"); } catch (Exception ex) { client.Shutdown(SocketShutdown.Both); DeleEvent.sendMessage -= SendMessage; client.Close(); receive.Abort(); break; } } }
public Client(Socket client, int clientID) { DeleEvent.AddEvent(SendMessage); this.client = client; this.clientID = clientID; receive = new Thread(ReceiveMessage); receive.IsBackground = true; receive.Start(); }