BytesRoad.Net.Sockets.SocketEx.BeginReceive C# (CSharp) Метод

BeginReceive() публичный Метод

Begins an asynchronous receive operation.
The BeginReceive method starts an asynchronous receive operation. It returns immediately and does not wait for the asynchronous call to complete.

The BytesRoad.Net.Sockets.SocketEx.EndReceive method is used to retrieve the results of the asynchronous call. It can be called any time after BeginReceive; if the asynchronous call has not completed, EndReceive will block until it completes.

The receive operation will not completed until the data is available for reading or error occurs. 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 object was disposed. /// /// buffer is a null reference (Nothing in Visual Basic). /// /// offset is less than 0. /// -or- /// offset is greater than the length of buffer. /// -or- /// size is less than 0. /// -or- /// size is greater than the length of buffer minus /// the value of the offset parameter. ///
public BeginReceive ( byte buffer, int offset, int size, AsyncCallback callback, object state ) : IAsyncResult
buffer byte Buffer to store the received data.
offset int The location in buffer to store the received data.
size int The number of bytes to receive.
callback AsyncCallback /// The AsyncCallback delegate. ///
state object /// An object containing state information for this request. ///
Результат IAsyncResult
        public IAsyncResult BeginReceive(
            byte[] buffer,
                                            int offset,
                                            int size,
                                            AsyncCallback callback,
                                            object state)
        {
            return (IAsyncResult)BeginTimeoutOp(
                _recvTimeout, 
                new Receive_Op(_baseSocket, buffer, offset, size),
                callback, state);
        }

Usage Example

Пример #1
0
        /// <summary>
        /// Begins an asynchronous reading 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>
        /// <param name="callback">
        /// The <see cref="System.AsyncCallback">AsyncCallback</see> delegate.
        /// </param>
        /// <param name="state">
        /// An object containing state information for this request.
        /// </param>
        /// <returns>
        /// An <see cref="System.IAsyncResult">IAsyncResult</see>
        /// that references the asynchronous read.
        /// </returns>
        /// <remarks>
        /// The <b>BeginRead</b> method starts an asynchronous
        /// read operation from the network stream.
        /// It returns immediately and does not wait for
        /// the asynchronous call to complete.
        /// <para>
        /// The
        /// <see cref="BytesRoad.Net.Sockets.NetworkStreamEx.EndRead"/>
        /// method is used to retrieve the results of
        /// the asynchronous call. It can be called
        /// any time after <b>BeginRead</b>; if the asynchronous
        /// call has not completed,
        /// <b>EndRead</b>
        /// will block until it completes.
        /// </para>
        /// <para>
        /// The read operation will not completed until the number of bytes specified by <i>size</i>
        /// parameter is read from the stream. 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>
        ///
        /// <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>
        public override IAsyncResult BeginRead(
            byte[] buffer,
            int offset,
            int size,
            AsyncCallback callback,
            object state
            )
        {
            CheckDisposed();
            _asyncCtx.SetProgress(true);
            try
            {
                Read_SO stateObj = new Read_SO(
                    buffer,
                    offset,
                    size,
                    callback,
                    state);

                return(_socket.BeginReceive(
                           buffer,
                           offset,
                           size,
                           new AsyncCallback(Read_End),
                           stateObj));
            }
            catch
            {
                _asyncCtx.SetProgress(false);
                CheckDisposed();
                throw;
            }
        }