System.Net.Sockets.NetworkStream.EndRead C# (CSharp) Метод

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

public EndRead ( IAsyncResult asyncResult ) : int
asyncResult IAsyncResult
Результат int
        public int EndRead(IAsyncResult asyncResult)
        {
#if DEBUG
            using (DebugThreadTracking.SetThreadKind(ThreadKinds.User))
            {
#endif
                if (_cleanedUp)
                {
                    throw new ObjectDisposedException(this.GetType().FullName);
                }

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

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

                try
                {
                    int bytesTransferred = chkStreamSocket.EndReceive(asyncResult);
                    return bytesTransferred;
                }
                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
 private void ReadNetworkData(NetworkStream stream)
 {
     try
     {
         stream.BeginRead(_buffer, 0, _buffer.Length, ar =>
         {
             ParseBuffer(stream.EndRead(ar));
             ReadNetworkData(stream);
         }, stream);
     }
     catch
     {
     }
 }
All Usage Examples Of System.Net.Sockets.NetworkStream::EndRead