System.Net.Sockets.NetworkStream.BeginRead C# (CSharp) Method

BeginRead() public method

public BeginRead ( byte buffer, int offset, int size, AsyncCallback callback, Object state ) : IAsyncResult
buffer byte
offset int
size int
callback AsyncCallback
state Object
return IAsyncResult
        public IAsyncResult BeginRead(byte[] buffer, int offset, int size, AsyncCallback callback, Object state)
        {
#if DEBUG
            using (DebugThreadTracking.SetThreadKind(ThreadKinds.User | ThreadKinds.Async))
            {
#endif
                bool canRead = CanRead; // Prevent race with Dispose.
                if (_cleanedUp)
                {
                    throw new ObjectDisposedException(this.GetType().FullName);
                }
                if (!canRead)
                {
                    throw new InvalidOperationException(SR.net_writeonlystream);
                }

                // 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));
                }

                Socket chkStreamSocket = _streamSocket;
                if (chkStreamSocket == null)
                {
                    throw new IOException(SR.Format(SR.net_io_readfailure, SR.net_io_connectionclosed));
                }

                try
                {
                    IAsyncResult asyncResult =
                        chkStreamSocket.BeginReceive(
                            buffer,
                            offset,
                            size,
                            SocketFlags.None,
                            callback,
                            state);

                    return asyncResult;
                }
                catch (Exception exception)
                {
                    if (exception is OutOfMemoryException)
                    {
                        throw;
                    }

                    // Some sort of error occurred on the socket call,
                    // set the SocketException as InnerException and throw.
                    throw new IOException(SR.Format(SR.net_io_readfailure, exception.Message), exception);
                }
#if DEBUG
            }
#endif
        }

Usage Example

示例#1
0
        public RiceClient(TcpClient tcp, RiceListener parent, bool exchangeRequired)
        {
            this.tcp = tcp;
            this.parent = parent;
            this.exchangeRequired = exchangeRequired;

            ns = tcp.GetStream();
            Alive = true;

            try
            {
                if (exchangeRequired)
                {
                    buffer = new byte[56];
                    bytesToRead = buffer.Length;
                    ns.BeginRead(buffer, 0, 56, onExchange, null);
                }
                else
                {
                    buffer = new byte[4];
                    bytesToRead = buffer.Length;
                    ns.BeginRead(buffer, 0, 4, onHeader, null);
                }
            }
            catch (Exception ex) { Kill(ex); }
        }
All Usage Examples Of System.Net.Sockets.NetworkStream::BeginRead