HandleResult OnReceive(TcpClient sender, byte[] bytes) { // 数据到达了 if (isSendFile == true) { // 如果发送了文件,并接收到了返回数据 isSendFile = false; MyFileInfo myFile = client.BytesToStruct <MyFileInfo>(bytes); int objSize = Marshal.SizeOf(myFile); // 因为没有附加尾数据,所以大小可以用length - objSize byte[] contentBytes = new byte[bytes.Length - objSize]; Array.ConstrainedCopy(bytes, objSize, contentBytes, 0, contentBytes.Length); string txt = Encoding.Default.GetString(contentBytes); string msg = string.Empty; if (txt.Length > 100) { msg = txt.Substring(0, 100) + "......"; } else { msg = txt; } AddMsg(string.Format(" > [{0},OnReceive] -> FileInfo(Path:\"{1}\",Size:{2})", sender.ConnectionId, myFile.FilePath, myFile.FileSize)); AddMsg(string.Format(" > [{0},OnReceive] -> FileContent(\"{1}\")", sender.ConnectionId, msg)); } else if (studentType != StudentType.None) { switch (studentType) { case StudentType.Array: Student[] students = sender.BytesToObject(bytes) as Student[]; foreach (var stu in students) { AddMsg(string.Format(" > [{0},OnReceive] -> Student({1},{2},{3})", sender.ConnectionId, stu.Id, stu.Name, stu.GetSexString())); } break; case StudentType.List: List <Student> stuList = sender.BytesToObject(bytes) as List <Student>; foreach (var stu in stuList) { AddMsg(string.Format(" > [{0},OnReceive] -> Student({1},{2},{3})", sender.ConnectionId, stu.Id, stu.Name, stu.GetSexString())); } break; case StudentType.Single: Student student = sender.BytesToObject(bytes) as Student; AddMsg(string.Format(" > [{0},OnReceive] -> Student({1},{2},{3})", sender.ConnectionId, student.Id, student.Name, student.GetSexString())); studentType = StudentType.None; break; } } else { AddMsg(string.Format(" > [{0},OnReceive] -> ({1} bytes)", sender.ConnectionId, bytes.Length)); } return(HandleResult.Ok); }