private void receiveCallback(IAsyncResult ar) { int dataReceivedSize = 0; try { dataReceivedSize = clientSocket.EndReceive(ar); } catch (Exception e) { if (!clientSocket.Connected) { onClientDisconnected(e.Message); } } ReceiveBuffer receiveBuffer = (ReceiveBuffer)ar.AsyncState; if (dataReceivedSize > 0) { receiveBuffer.Append(dataReceivedSize); if (clientSocket.Available > 0) { clientSocket.BeginReceive(receiveBuffer.tempBuffer, 0, ReceiveBuffer.BufferSize, SocketFlags.None, receiveCallback, receiveBuffer); } else { object data = receiveBuffer.Deserialize(); onDataReceived(data); receiveData(); } } }
private void receiveData() { if (clientSocket.Connected) { ReceiveBuffer receiveBuffer = new ReceiveBuffer(); clientSocket.BeginReceive(receiveBuffer.tempBuffer, 0, ReceiveBuffer.BufferSize, SocketFlags.None, receiveCallback, receiveBuffer); } }