static void ReceiveCallBack(IAsyncResult ar) { Socket clientSocket = null; try { clientSocket = ar.AsyncState as Socket; int count = clientSocket.EndReceive(ar); if (count == 0) { clientSocket.Close(); return; } // 更新存储了多少数据长度 msg.AddIndex(count); // 装换byte到string //string clientMsgReceive = Encoding.UTF8.GetString(dataBuffer, 0, count); //Console.WriteLine("从客户端接受到消息:" + clientMsgReceive); msg.ReadMessage(); // 循环监听 clientSocket.BeginReceive(msg.Data, msg.StartIndex, msg.RemainSize, SocketFlags.None, ReceiveCallBack, clientSocket); } catch (Exception e) { if (clientSocket != null) { clientSocket.Close(); } Console.WriteLine("客户端未正常关闭:" + e); } }