private void ReceiveResult(IAsyncResult ar) { socket = (System.Net.Sockets.UdpClient)ar.AsyncState; IPEndPoint remote = new IPEndPoint(IPAddress.Any, 0); recvBuffer = socket.EndReceive(ar, ref remote); dataPacker.UnPack(recvBuffer); socket.BeginReceive(ReceiveResult, socket); }
private void ReadResult(IAsyncResult ar) { System.Net.Sockets.TcpClient tcpClient = (System.Net.Sockets.TcpClient)ar.AsyncState; NetworkStream stream = tcpClient.GetStream(); int recvLength = stream.EndRead(ar); if (recvLength <= 0) { Debug.Log("远程客户端:" + tcpClient.Client.RemoteEndPoint + "已经断开"); remoteClients.Remove(tcpClient); tcpClient.Close(); return; } byte[] recvBytes = new byte[recvLength]; Array.Copy(recvBuffer, 0, recvBytes, 0, recvLength); dataPacker.UnPack(recvBytes); stream.BeginRead(recvBuffer, 0, recvBuffer.Length, ReadResult, tcpClient); }
private void ReadResult(IAsyncResult ar) { socket = (System.Net.Sockets.TcpClient)ar.AsyncState; if (socket.Connected) { NetworkStream stream = socket.GetStream(); int recvLength = stream.EndRead(ar); if (recvLength > 0) { byte[] recvBytes = new byte[recvLength]; Array.Copy(recvBuffer, 0, recvBytes, 0, recvLength); dataPacker.UnPack(recvBytes); stream.BeginRead(recvBuffer, 0, recvBuffer.Length, ReadResult, socket); } else { Debug.LogError("网络中断"); Close(); } } }