This documentation is archived and is not being maintained.

NetworkStream.DataAvailable Property

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

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

public virtual bool DataAvailable { get; }
/** @property */
public boolean get_DataAvailable ()

public function get DataAvailable () : boolean

Property Value

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

Exception typeCondition

ObjectDisposedException

The NetworkStream is closed.

IOException

The underlying Socket is closed.

SocketException

Use the SocketException.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.

Use the DataAvailable property to determine if data is ready to be read. If DataAvailable is true, a call to Read returns immediately. If the remote host shuts down or closes the connection, DataAvailable may throw a SocketException.

The following code example reads from the NetworkStream as long as data is available.

           
// 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
Show: