This documentation is archived and is not being maintained.

NetworkStream.DataAvailable Property

Gets a value indicating whether data is available on the NetworkStream to be read.

[Visual Basic]
Public Overridable ReadOnly Property DataAvailable As Boolean
[C#]
public virtual bool DataAvailable {get;}
[C++]
public: __property virtual bool get_DataAvailable();
[JScript]
public function get DataAvailable() : Boolean;

Property Value

true if data is available on the stream to be read; otherwise, false.

Exceptions

Exception Type Condition
ObjectDisposedException The NetworkStream is closed.
IOException The underlying Socket is closed.
SocketException Use SocketException.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.

Remarks

The Read method blocks execution until data is received from the remote host and queued in the network buffer for reading. If you want to avoid blocking, use the DataAvailable method to determine if this data is available. If DataAvailable is true, a call to Read will return immediately. If the remote host shuts down or closes the connection, DataAvailable throws a SocketException.

Example

[Visual Basic, C#, C++] The following example reads from the NetworkStream as long as data is available.

[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 | Read

Show: