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

BeginReceive() public method

public BeginReceive ( 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 BeginReceive(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 receive with this asyncResult.
            errorCode = DoBeginReceive(buffers, socketFlags, asyncResult);

            if (errorCode != SocketError.Success && errorCode != SocketError.IOPending)
            {
                asyncResult = null;
            }
            else
            {
                // 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.ReceiveClosureCache);
            }

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

Same methods

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

Usage Example

Esempio n. 1
0
    private void OnConnect(IAsyncResult ar)
    {
        try {
            clientSocket.EndConnect(ar);
            // here we are connected, so we send login request!
            byte[]  bytesMsg;
            Message loginMsg = new Message(Command.Login, txtName.Text);
            bytesMsg = loginMsg.toByte();

            clientSocket.BeginSend(bytesMsg,
                                   0,
                                   bytesMsg.Length,
                                   SocketFlags.None,
                                   new AsyncCallback(OnSend),
                                   null);
            clientSocket.BeginReceive(byteData,
                                      0,
                                      byteData.Length,
                                      SocketFlags.None,
                                      new AsyncCallback(OnReceive),
                                      null);
        }
        catch (Exception ex) {
            log("Something went wrong during establishing connection:");
            log(ex.Message);
            btnConnect.Sensitive    = true;
            btnDisconnect.Sensitive = false;
            btnMsg.Sensitive        = false;
        }
    }
All Usage Examples Of System.Net.Sockets.Socket::BeginReceive