System.Net.Sockets.NetworkStream.Read Method

Reads data from the System.Net.Sockets.NetworkStream.

Syntax

public override int Read (byte[] buffer, int offset, int size)

Parameters

buffer
An array of type byte that is the location in memory to store data read from the System.Net.Sockets.NetworkStream.
offset
The location in buffer to begin storing the data to.
size
The number of bytes to read from the System.Net.Sockets.NetworkStream.

Returns

The number of bytes read from the System.Net.Sockets.NetworkStream.

Exceptions

TypeReason
ArgumentNullException buffer is null.
ArgumentOutOfRangeException

offset < 0.

-or-

offset > buffer.Length.

-or-

size < 0.

-or-

size > buffer.Length - offset.

System.IO.IOException
Note: This method catches all exceptions thrown by the Socket.Receive(Byte[], int, SocketFlags) method.
ObjectDisposedExceptionThe current instance has been disposed.

Remarks

This method reads data into the buffer parameter and returns the number of bytes successfully read. If no data is available for reading, the NetworkStream.Read(Byte[], int, int) method returns 0. The NetworkStream.Read(Byte[], int, int) operation reads as much data as is available, up to the number of bytes specified by the size parameter. If the remote host shuts down the connection, and all available data has been received, the NetworkStream.Read(Byte[], int, int) method completes immediately and return zero bytes.

Note:

Check to see if the System.Net.Sockets.NetworkStream is readable by calling the NetworkStream.CanRead property. If you attempt to read from a System.Net.Sockets.NetworkStream that is not readable, you will get an System.IO.IOException.

Note:

If you receive an System.IO.IOException, check the Exception.InnerException property to determine if it was caused by a System.Net.Sockets.SocketException. If so, use the SocketException.ErrorCode property to obtain the specific error code, and refer to the Windows Sockets version 2 API error code documentation in MSDN for a detailed description of the error.

Requirements

Namespace: System.Net.Sockets
Assembly: System (in System.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0, 4.0.0.0