Receive() public method

Receives data from the remote host and store it in the specified buffer.
If no data is available for reading, the Receive method will block until data is available. You can use the BytesRoad.Net.Sockets.SocketEx.Available property to determine if data is available for reading. When BytesRoad.Net.Sockets.SocketEx.Available is non-zero, retry the receive operation.

The Receive method will read as much data as is available, up to the size of the buffer. If the remote host shuts down the BytesRoad.Net.Sockets.SocketEx connection with the BytesRoad.Net.Sockets.SocketEx.Shutdown method, and all available data has been received, the Receive method will complete immediately and return zero bytes.

/// The object was disposed. /// /// buffer is a null reference (Nothing in Visual Basic). /// /// An error occurred when attempting to access /// the socket which is used to complete the requested operation. ///
public Receive ( byte buffer ) : int
buffer byte /// Buffer to store the received data. ///
return int
Example #1
0
        /// <summary>
        /// Reads data from the network stream.
        /// </summary>
        /// <param name="buffer">The buffer to store data to.</param>
        /// <param name="offset">The location in the <i>buffer</i> where to start storing the received data.</param>
        /// <param name="size">The number of bytes to read.</param>
        /// <returns>The number of bytes read from the network stream.</returns>
        ///
        /// <exception cref="System.ObjectDisposedException">
        /// The <see cref="BytesRoad.Net.Sockets.NetworkStreamEx"/> object was disposed.
        /// </exception>
        ///
        /// <exception cref="System.ArgumentNullException">
        /// <i>buffer</i> is a null reference (<b>Nothing</b> in Visual Basic).
        /// </exception>
        ///
        /// <exception cref="System.ArgumentOutOfRangeException">
        /// <i>offset</i> is less than 0.
        /// <para>-or-</para>
        /// <i>offset</i> is greater than the length of <i>buffer</i>.
        /// <para>-or-</para>
        /// <i>size</i> is less than 0.
        /// <para>-or-</para>
        /// <i>size</i> is greater than the length of <i>buffer</i> minus
        /// the value of the <i>offset</i> parameter.
        /// </exception>
        ///
        /// <exception cref="System.Net.Sockets.SocketException">
        /// An error occurred when attempting to access
        /// the socket which is used to complete the requested operation.
        /// </exception>
        /// <remarks>
        /// If no data is available for reading, the <b>Read</b> method will block
        /// until data is available. You can use the
        /// <see cref="BytesRoad.Net.Sockets.NetworkStreamEx.DataAvailable"/>
        /// property to determine if data is available for reading. When
        /// <see cref="BytesRoad.Net.Sockets.NetworkStreamEx.DataAvailable"/>
        /// is non-zero, retry the receive operation.
        /// <para>
        /// The <b>Read</b> method will
        /// read as much data as specified by <i>size</i> parameter. If
        /// the remote host shuts down the
        /// <see cref="BytesRoad.Net.Sockets.SocketEx"/>
        /// connection with the
        /// <see cref="BytesRoad.Net.Sockets.SocketEx.Shutdown"/>
        /// method, and all available data has been received, the <b>Read</b> method
        /// will complete and return number of bytes was read.
        /// </para>
        /// <note>
        /// The <b>NetworkStreamEx</b> should have access right to read data from the network stream.
        /// You may use <see cref="BytesRoad.Net.Sockets.NetworkStreamEx.CanRead"/> property to check this.
        /// </note>
        /// </remarks>
        public override int Read(byte[] buffer, int offset, int size)
        {
            CheckDisposed();
            _asyncCtx.SetProgress(true);
            try
            {
                int read = 0;
                int num  = 1;
                while ((num > 0) && (read < size))
                {
                    num   = _socket.Receive(buffer, offset + read, size - read);
                    read += num;
                }

                return(read);
            }
            catch
            {
                CheckDisposed();
                throw;
            }
            finally
            {
                _asyncCtx.SetProgress(false);
            }
        }