System.Net.Sockets.Socket.BeginReceive C# (CSharp) Метод

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

public BeginReceive ( byte buffer, int offset, int size, SocketFlags socketFlags, SocketError &errorCode, AsyncCallback callback, object state ) : IAsyncResult
buffer byte
offset int
size int
socketFlags SocketFlags
errorCode SocketError
callback AsyncCallback
state object
Результат IAsyncResult
        public IAsyncResult BeginReceive(byte[] buffer, int offset, int size, 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 (buffer == null)
            {
                throw new ArgumentNullException(nameof(buffer));
            }
            if (offset < 0 || offset > buffer.Length)
            {
                throw new ArgumentOutOfRangeException(nameof(offset));
            }
            if (size < 0 || size > buffer.Length - offset)
            {
                throw new ArgumentOutOfRangeException(nameof(size));
            }

            // 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(buffer, offset, size, 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 ( IList buffers, SocketFlags socketFlags, SocketError &errorCode, AsyncCallback callback, object state ) : IAsyncResult
Socket::BeginReceive ( byte buffer, int offset, int size, SocketFlags socketFlags, AsyncCallback callback, object state ) : IAsyncResult

Usage Example

Пример #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