System.Net.Sockets.Socket.BeginSend C# (CSharp) Method

BeginSend() public method

public BeginSend ( IList buffers, SocketFlags socketFlags, SocketError &errorCode, AsyncCallback callback, object state ) : IAsyncResult
buffers IList
socketFlags SocketFlags
errorCode SocketError
callback AsyncCallback
state object
return IAsyncResult
        public IAsyncResult BeginSend(IList<ArraySegment<byte>> buffers, SocketFlags socketFlags, out SocketError errorCode, AsyncCallback callback, object state)
        {
            if (NetEventSource.IsEnabled) NetEventSource.Enter(this);
            if (CleanedUp)
            {
                throw new ObjectDisposedException(this.GetType().FullName);
            }

            // Validate input parameters.
            if (buffers == null)
            {
                throw new ArgumentNullException(nameof(buffers));
            }

            if (buffers.Count == 0)
            {
                throw new ArgumentException(SR.Format(SR.net_sockets_zerolist, nameof(buffers)), nameof(buffers));
            }

            // We need to flow the context here.  But we don't need to lock the context - we don't use it until the callback.
            OverlappedAsyncResult asyncResult = new OverlappedAsyncResult(this, state, callback);
            asyncResult.StartPostingAsyncOp(false);

            // Run the send with this asyncResult.
            errorCode = DoBeginSend(buffers, socketFlags, asyncResult);

            // We're not throwing, so finish the async op posting code so we can return to the user.
            // If the operation already finished, the callback will be called from here.
            asyncResult.FinishPostingAsyncOp(ref Caches.SendClosureCache);

            if (errorCode != SocketError.Success && errorCode != SocketError.IOPending)
            {
                asyncResult = null;
            }

            if (NetEventSource.IsEnabled) NetEventSource.Exit(this, asyncResult);
            return asyncResult;
        }

Same methods

Socket::BeginSend ( IList buffers, SocketFlags socketFlags, AsyncCallback callback, object state ) : IAsyncResult
Socket::BeginSend ( byte buffer, int offset, int size, SocketFlags socketFlags, AsyncCallback callback, object state ) : IAsyncResult
Socket::BeginSend ( byte buffer, int offset, int size, SocketFlags socketFlags, SocketError &errorCode, AsyncCallback callback, object state ) : IAsyncResult

Usage Example

        public void SendMessageToServer(string argCommandString)
        {
            // create a Packet object from the passed data; this packet can be any object type because we use serialization!
            //Dim mPacket As New Dictionary(Of String, String)
            //mPacket.Add("CMD", argCommandString)
            //mPacket.Add("MSG", argMessageString)
            string mPacket = argCommandString;

            // serialize the Packet into a stream of bytes which is suitable for sending with the Socket
            System.Runtime.Serialization.Formatters.Binary.BinaryFormatter mSerializer = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
            System.IO.MemoryStream mSerializerStream = new System.IO.MemoryStream();
            mSerializer.Serialize(mSerializerStream, mPacket);

            // get the serialized Packet bytes
            byte[] mPacketBytes = mSerializerStream.GetBuffer();

            // convert the size into a byte array
            byte[] mSizeBytes = BitConverter.GetBytes(mPacketBytes.Length + 4);

            // create the async state object which we can pass between async methods
            SocketGlobals.AsyncSendState mState = new SocketGlobals.AsyncSendState(cClientSocket);

            // resize the BytesToSend array to fit both the mSizeBytes and the mPacketBytes
            // ERROR: Not supported in C#: ReDimStatement
            Array.Resize(ref mState.BytesToSend, mPacketBytes.Length + mSizeBytes.Length);

            // copy the mSizeBytes and mPacketBytes to the BytesToSend array
            System.Buffer.BlockCopy(mSizeBytes, 0, mState.BytesToSend, 0, mSizeBytes.Length);
            System.Buffer.BlockCopy(mPacketBytes, 0, mState.BytesToSend, mSizeBytes.Length, mPacketBytes.Length);

            cClientSocket.BeginSend(mState.BytesToSend, mState.NextOffset(), mState.NextLength(), SocketFlags.None, new AsyncCallback(MessagePartSent), mState);
        }
All Usage Examples Of System.Net.Sockets.Socket::BeginSend