NetworkStream.DataAvailable Propriété

Définition

Obtient une valeur indiquant si des données sont disponibles sur le NetworkStream à lire.Gets a value that indicates whether data is available on the NetworkStream to be read.

public:
 virtual property bool DataAvailable { bool get(); };
public virtual bool DataAvailable { get; }
member this.DataAvailable : bool
Public Overridable ReadOnly Property DataAvailable As Boolean

Valeur de propriété

Boolean

true si des données sont disponibles dans le flux à lire ; sinon, false.true if data is available on the stream to be read; otherwise, false.

Exceptions

Le NetworkStream est fermé.The NetworkStream is closed.

Le Socket sous-jacent est fermé.The underlying Socket is closed.

Utilisez la propriété ErrorCode pour obtenir le code d’erreur, et reportez-vous à la documentation sur les codes d’erreur de l’API Windows Sockets version 2 pour obtenir une description détaillée de l’erreur.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.

Exemples

L’exemple de code suivant lit à partir du NetworkStream tant que des données sont disponibles.The following code example reads from the NetworkStream as long as data is available.

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

Remarques

Utilisez la DataAvailable propriété pour déterminer si les données sont prêtes à être lues.Use the DataAvailable property to determine if data is ready to be read. Si DataAvailable est true , un appel à est Read retourné immédiatement.If DataAvailable is true, a call to Read returns immediately. Si l’hôte distant arrête ou ferme la connexion, DataAvailable peut lever une SocketException .If the remote host shuts down or closes the connection, DataAvailable may throw a SocketException.

S’applique à

Voir aussi