NetworkStream.Read Method (Byte[], Int32, Int32)


Reads data from the NetworkStream.

Namespace:   System.Net.Sockets
Assembly:  System (in System.dll)

override Read : 
        buffer:byte[] *
        offset:int *
        size:int -> int


Type: System.Byte[]

An array of type Byte that is the location in memory to store data read from the NetworkStream.

Type: System.Int32

The location in buffer to begin storing the data to.

Type: System.Int32

The number of bytes to read from the NetworkStream.

Return Value

Type: System.Int32

The number of bytes read from the NetworkStream.

Exception Condition

The buffer parameter is null.


The offset parameter is less than 0.


The offset parameter is greater than the length of buffer.


The size parameter is less than 0.


The size parameter is greater than the length of buffer minus the value of the offset parameter.


An error occurred when accessing the socket. See the Remarks section for more information.


The underlying Socket is closed.


The NetworkStream is closed.


There is a failure reading from the network.

This method reads data into the buffer parameter and returns the number of bytes successfully read. If no data is available for reading, the Read method returns 0. The Read 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 Read method completes immediately and return zero bytes.


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


If you receive an IOException, check the InnerException property to determine if it was caused by a SocketException. If so, use the 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.

The following code example uses DataAvailable to determine if data is available to be read. If data is available, it reads from the NetworkStream.

No code example is currently available or this language may not be supported.

.NET Framework
Available since 1.1
Return to top