Org.Mentalis.Security.Ssl.SecureSocket.BeginSend C# (CSharp) Method

BeginSend() public method

Sends data asynchronously to a connected SecureSocket.
is a null reference (Nothing in Visual Basic). An operating system error occurs while accessing the SecureSocket. The specified offset or size exceeds the size of buffer. The specified size is zero. The SecureSocket has been closed. An error occurred while encrypting the data.
public BeginSend ( byte buffer, int offset, int size, SocketFlags socketFlags, AsyncCallback callback, object state ) : IAsyncResult
buffer byte The data to send.
offset int The zero-based position in the buffer parameter at which to begin sending data.
size int The number of bytes to send.
socketFlags SocketFlags A bitwise combination of the values.
callback AsyncCallback The delegate.
state object An object containing state information for this request.
return IAsyncResult
		public override IAsyncResult BeginSend(byte[] buffer, int offset, int size, SocketFlags socketFlags, AsyncCallback callback, object state) {
			if (SecureProtocol == SecureProtocol.None)
				return base.BeginSend(buffer, offset, size, socketFlags, callback, state);
			if (!Connected)
				throw new SocketException();
			if (buffer == null)
				throw new ArgumentNullException();
			if (size == 0)
				throw new ArgumentException();
			if (offset < 0 || offset >= buffer.Length || size > buffer.Length - offset || size < 0)
				throw new ArgumentOutOfRangeException();
			// begin secure send
			return m_Controller.BeginSend(buffer, offset, size, callback, state);
		}
		/// <summary>

Usage Example

        /// <summary>
        /// Begins an asynchronous write to a stream.
        /// </summary>
        /// <param name="buffer">The location in memory that holds the data to send.</param>
        /// <param name="offset">The location in buffer to begin sending the data.</param>
        /// <param name="size">The size of buffer.</param>
        /// <param name="callback">The delegate to call when the asynchronous call is complete.</param>
        /// <param name="state">An object containing additional information supplied by the client.</param>
        /// <returns>An <see cref="IAsyncResult"/> representing the asynchronous call.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="buffer"/> is a null reference (<b>Nothing</b> in Visual Basic).</exception>
        /// <exception cref="ArgumentOutOfRangeException">The specified <paramref name="offset"/> or <paramref name="size"/> exceeds the size of <paramref name="buffer"/>.</exception>
        /// <exception cref="IOException">There is a failure while writing to the network.</exception>
        // Thanks go out to Martin Plante for notifying us about a bug in this method.
        public override IAsyncResult BeginWrite(byte[] buffer, int offset, int size, AsyncCallback callback, object state)
        {
            if (buffer == null)
            {
                throw new ArgumentNullException();
            }
            if (offset < 0 || offset > buffer.Length || size < 0 || size > buffer.Length - offset)
            {
                throw new ArgumentOutOfRangeException();
            }
            if (Socket == null)
            {
                throw new IOException();
            }
            if (WriteResult != null)
            {
                throw new IOException();
            }
            TransferItem localResult = new TransferItem(new byte[size], 0, size, new AsyncResult(callback, state, null), DataType.ApplicationData);

            WriteResult = localResult;
            Array.Copy(buffer, offset, localResult.Buffer, 0, size);
            try {
                Socket.BeginSend(localResult.Buffer, 0, size, SocketFlags.None, new AsyncCallback(OnBytesSent), (int)0);
                return(localResult.AsyncResult);
            } catch {
                throw new IOException();
            }
        }