Export (0) Print
Expand All

NetworkStream.Read Method

Reads data from the NetworkStream.

[Visual Basic]
Overrides Public Function Read( _
   <InteropServices.In(), _
   Out()> ByVal buffer() As Byte, _
   ByVal offset As Integer, _
   ByVal size As Integer _
) As Integer
[C#]
public override int Read(
   [
   In,
   Out
] byte[] buffer,
 int offset,
 int size
);
[C++]
public: int Read(
   [
   In,
   Out
] unsigned char buffer __gc[],
 int offset,
 int size
);
[JScript]
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.

Exceptions

Exception Type Condition
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 less than the length of buffer minus the value of the offset parameter.

IOException The underlying Socket is closed.
ObjectDisposedException The NetworkStream is closed.

-or-

There is a failure reading from the network.

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

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 method will block until data is available. To avoid blocking, you can use the DataAvailable property to determine if data is queued in the incoming network buffer for reading. If DataAvailable returns true, the Read operation will complete immediately. The Read operation will read 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 will complete immediately and return zero bytes.

Note   Check to see if the NetworkStream is readable by calling CanRead. If you attempt to read from a NetworkStream that is not readable, you will get an IOException.
Note   If you receive a IOException check the InnerException property to determine if it was caused by a SocketException. If so, use ErrorCode to obtain the specific error code. Once you have obtained this code, you can refer to the Windows Socket Version 2 API error code documentation in MSDN for a detailed description of the error.

Example

[Visual Basic, C#, C++] The following example uses DataAvailable to determine if data is available to be read. If data is available, it reads from the NetworkStream.

[Visual Basic] 
' Check to see if this NetworkStream is readable.
If myNetworkStream.CanRead Then
   Dim myReadBuffer(1024) As Byte
   Dim myCompleteMessage As [String] = ""
   Dim numberOfBytesRead As Integer = 0
   
   ' Incoming message may be larger than the buffer size.
   Do
      numberOfBytesRead = myNetworkStream.Read(myReadBuffer, 0, myReadBuffer.Length)
      myCompleteMessage = [String].Concat(myCompleteMessage, Encoding.ASCII.GetString(myReadBuffer, 0, numberOfBytesRead))
   Loop 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.")
End If


[C#] 

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

     // Incoming message may be larger than the buffer size.
     do{
          numberOfBytesRead = myNetworkStream.Read(myReadBuffer, 0, myReadBuffer.Length);  
          myCompleteMessage = 
              String.Concat(myCompleteMessage, 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.");
 }


[C++] 

        // Check to see if this NetworkStream is readable.
        if (myNetworkStream->CanRead) {
            Byte myReadBuffer[] = new Byte[1024];
            String* myCompleteMessage = S"";
            int numberOfBytesRead = 0;

            // Incoming message may be larger than the buffer size.
            do{
                numberOfBytesRead = myNetworkStream->Read(myReadBuffer, 0, myReadBuffer->Length);  
                myCompleteMessage = 
                    String::Concat(myCompleteMessage, Encoding::ASCII->GetString(myReadBuffer, 0, numberOfBytesRead));  
            } while (myNetworkStream->DataAvailable);

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

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework, Common Language Infrastructure (CLI) Standard

See Also

NetworkStream Class | NetworkStream Members | System.Net.Sockets Namespace | Receive | Using Streams on the Network | DataAvailable | CanRead

Show:
© 2014 Microsoft