Export (0) Print
Expand All
1 out of 5 rated this helpful - Rate this topic

NetworkStream.Read Method

Reads data from the NetworkStream.

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

public override int Read (
	[InAttribute] [OutAttribute] byte[] buffer,
	int offset,
	int size
)
public int Read (
	/** @attribute InAttribute() */ /** @attribute OutAttribute() */ byte[] buffer, 
	int offset, 
	int size
)
public override function Read (
	buffer : byte[], 
	offset : int, 
	size : int
) : int

Parameters

buffer

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

offset

The location in buffer to begin storing the data to.

size

The number of bytes to read from the NetworkStream.

Return Value

The number of bytes read from the NetworkStream.
Exception typeCondition

ArgumentNullException

buffer is a null reference (Nothing in Visual Basic).

ArgumentOutOfRangeException

offset is less than 0.

-or-

offset is greater than the length of buffer.

-or-

size is less than 0.

-or-

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

-or-

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

IOException

The underlying Socket is closed.

ObjectDisposedException

The NetworkStream is closed.

-or-

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.

NoteNote

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.

NoteNote

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.

           
// Check to see if this NetworkStream is readable.
if(myNetworkStream.CanRead){
    byte[] myReadBuffer = new byte[1024];
    StringBuilder myCompleteMessage = new StringBuilder();
    int numberOfBytesRead = 0;

    // Incoming message may be larger than the buffer size.
    do{
         numberOfBytesRead = myNetworkStream.Read(myReadBuffer, 0, myReadBuffer.Length);

         myCompleteMessage.AppendFormat("{0}", Encoding.ASCII.GetString(myReadBuffer, 0, numberOfBytesRead));
                         
    }
    while(myNetworkStream.DataAvailable);

    // Print out the received message to the console.
    Console.WriteLine("You received the following message : " +
                                 myCompleteMessage);
}
else{
     Console.WriteLine("Sorry.  You cannot read from this NetworkStream.");
}


// Check to see if this NetworkStream is readable.
if (myNetworkStream.get_CanRead()) {
    ubyte myReadBuffer[] = new ubyte[1024];
    String myCompleteMessage = "";
    int numberOfBytesRead = 0;
    // Incoming message may be larger than the buffer size.
    do {
        numberOfBytesRead = myNetworkStream.Read(myReadBuffer,
            0, myReadBuffer.get_Length());
        myCompleteMessage = String.Concat(myCompleteMessage, 
            Encoding.get_ASCII().GetString(myReadBuffer, 0, 
            numberOfBytesRead));
    } while (myNetworkStream.get_DataAvailable());
    // Print out the received message to the console.
    Console.WriteLine("You received the following message : " 
        + myCompleteMessage);
}
else {
    Console.WriteLine("Sorry.  You cannot read from this "
        + "NetworkStream.");
}

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.