1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
|
public void Recv(TcpClient t) { byte[] result = new byte[t.Available]; try { t.GetStream().BeginRead(result, 0, result.Length, new AsyncCallback(RecvCallback), t); } catch (Exception e) { PrintMsg("Error:" + e.ToString()); } }
public void RecvCallback(IAsyncResult ar) { TcpClient t = (TcpClient)ar.AsyncState; if (t.Connected) { NetworkStream ns = t.GetStream(); byte[] data = new byte[t.Available]; ns.Read(data, 0, t.Available); string content = Encoding.UTF8.GetString(data); PrintMsg("客户端接收到数据:" + content); } }
|