private void OnReceiveData(IAsyncResult result) { try { var readbytes = myStream.EndRead(result); if (readbytes <= 0) { //client is not connected to the server anymore CloseSocket(connectionID); return; } var newBytes = new byte[readbytes]; Buffer.BlockCopy(readBuff, 0, newBytes, 0, readbytes); ServerHandleData.HandleData(connectionID, newBytes); myStream.BeginRead(readBuff, 0, socket.ReceiveBufferSize, OnReceiveData, null); } catch (Exception) { CloseSocket(connectionID); } }
private void OnRecieveData(IAsyncResult result) { try { int length = Stream.EndRead(result); if (length <= 0) { CloseConnection(); return; } byte[] newBytes = new byte[length]; Array.Copy(recBuffer, newBytes, length); ServerHandleData.HandleData(connectionID, newBytes); Stream.BeginRead(recBuffer, 0, socket.ReceiveBufferSize, OnRecieveData, null); } catch (Exception) { CloseConnection(); return; } }