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

Definition

Reads data from the NetworkStream.

public:
 override int Read(cli::array <System::Byte> ^ buffer, int offset, int size);
public override int Read (byte[] buffer, int offset, int size);
override this.Read : byte[] * int * int -> int
Public Overrides Function Read (buffer As Byte(), offset As Integer, size As Integer) As Integer
Parameters
buffer
Byte[]

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

offset
Int32 Int32 Int32 Int32

The location in buffer to begin storing the data to.

size
Int32 Int32 Int32 Int32

The number of bytes to read from the NetworkStream.

Returns

The number of bytes read from the NetworkStream, or 0 if the socket is closed.

Exceptions

The offset parameter is less than 0.

-or-

The offset parameter is greater than the length of buffer.

-or-

The size parameter is less than 0.

-or-

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

-or-

An error occurred when accessing the socket.

The NetworkStream is closed.

-or-

There is a failure reading from the network.

Examples

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

// Examples for CanRead, Read, and DataAvailable.
// Check to see if this NetworkStream is readable.
if ( myNetworkStream->CanRead )
{
   array<Byte>^ myReadBuffer = gcnew array<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 : {0}",
      myCompleteMessage );
}
else
{
   Console::WriteLine( "Sorry.  You cannot read from this NetworkStream." );
}
      // Examples for CanRead, Read, and DataAvailable.
     
      // 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.");
      }

' Examples for CanRead, Read, and DataAvailable.
' Check to see if this NetworkStream is readable.
If myNetworkStream.CanRead Then
   Dim myReadBuffer(1024) As Byte
       Dim myCompleteMessage As StringBuilder = New StringBuilder()
   Dim numberOfBytesRead As Integer = 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))
   Loop While myNetworkStream.DataAvailable
   
   ' Print out the received message to the console.
       Console.WriteLine(("You received the following message : " + myCompleteMessage.ToString()))
Else
   Console.WriteLine("Sorry.  You cannot read from this NetworkStream.")
End If

Remarks

This method reads data into the buffer parameter and returns the number of bytes successfully read. If the socket is closed, 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.

Note

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.

Note

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 for a detailed description of the error.

Applies to

See Also