BytesRoad.Net.Sockets.SocketEx.BeginSend C# (CSharp) Method

BeginSend() public method

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

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

Send operation will not completed until all of the bytes in the buffer are sent. There is also no guarantee that the data you send will appear on the network immediately. To increase network efficiency, the underlying system may delay transmission until a significant amount of outgoing data is collected. A successful completion of the send operation means that the underlying system has had room to buffer your data for a network send.

/// 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. /// /// An error occurred when attempting to access /// the socket which is used to complete the requested operation. ///
public BeginSend ( byte buffer, int offset, int size, AsyncCallback callback, object state ) : IAsyncResult
buffer byte Data to send.
offset int The position in the data buffer at which to begin sending data.
size int The number of bytes to send.
callback AsyncCallback /// The AsyncCallback delegate. ///
state object /// An object containing state information for this request. ///
return IAsyncResult
        public IAsyncResult BeginSend(
            byte[] buffer,
                                        int offset,
                                        int size,
                                        AsyncCallback callback,
                                        object state)
        {
            return (IAsyncResult)BeginTimeoutOp(
                _sendTimeout, 
                new Send_Op(_baseSocket, buffer, offset, size),
                callback, state);
        }

Usage Example

Example #1
0
        /// <summary>
        /// Begins an asynchronous write to the network stream.
        /// </summary>
        /// <param name="buffer">Data to write.</param>
        /// <param name="offset">The position in the data <i>buffer</i> from which to begin writing.</param>
        /// <param name="size">The number of bytes to write.</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 write.
        /// </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>
        ///
        /// <remarks>
        /// The <b>BeginWrite</b> method starts an asynchronous
        /// write operation.
        /// It returns immediately and does not wait for
        /// the asynchronous call to complete.
        /// <para>
        /// The
        /// <see cref="BytesRoad.Net.Sockets.NetworkStreamEx.EndWrite"/>
        /// method is used to retrieve the results of
        /// the asynchronous call. It can be called
        /// any time after <b>BeginWrite</b>; if the asynchronous
        /// call has not completed,
        /// <b>EndWrite</b>
        /// will block until it completes.
        /// </para>
        /// <para>
        /// Write operation will not completed until all data, specified by <i>size</i> parameter, are sent.
        /// </para>
        ///
        /// <note>
        /// The <b>NetworkStreamEx</b> should have access right to write data to the network stream.
        /// You may use <see cref="BytesRoad.Net.Sockets.NetworkStreamEx.CanWrite"/> property to check this.
        /// </note>
        ///
        /// </remarks>
        public override IAsyncResult BeginWrite(
            byte[] buffer,
            int offset,
            int size,
            AsyncCallback callback,
            object state
            )
        {
            CheckDisposed();
            _asyncCtx.SetProgress(true);
            try
            {
                Write_SO stateObj = new Write_SO(
                    buffer,
                    offset,
                    size,
                    callback,
                    state);

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