1 private void AcceptMsg() { 2 //读取数据 3 NetworkStream ns = client.GetStream(); 4 //字组处理 5 while (true) { 6 try { 7 byte[] bytes = new byte[4096]; 8 byte[] sendBytes = new byte[4096]; 9 int bytesread = ns.Read(bytes, 0, bytes.Length); 10 string msg = Encoding.UTF8.GetString(bytes, 0, bytesread); 11 12 Console.WriteLine("我是客户端,接收消息如下:" + msg); 13 ns.Flush(); 14 } catch (System.Exception ex) { 15 throw new Exception(ex.ToString()); 16 } 17 } 18 }