NetworkStream.CanRead Свойство

Определение

Возвращает значение, указывающее, поддерживает ли объект NetworkStream чтение.Gets a value that indicates whether the NetworkStream supports reading.

public:
 virtual property bool CanRead { bool get(); };
public override bool CanRead { get; }
member this.CanRead : bool
Public Overrides ReadOnly Property CanRead As Boolean

Значение свойства

Boolean

Значение true, если данные могут быть прочитаны из потока; в противном случае — false.true if data can be read from the stream; otherwise, false. Значение по умолчанию — true.The default value is true.

Примеры

В следующем примере кода проверяется CanRead, чтобы убедиться, что NetworkStream доступен для чтения.The following code example checks CanRead to verify that the NetworkStream is readable. Затем он выполняет операцию чтения NetworkStream.It then performs a read operation on 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

Комментарии

Если CanRead true, NetworkStream допускает вызовы метода Read.If CanRead is true, NetworkStream allows calls to the Read method. Укажите соответствующее FileAccess перечислимое значение в конструкторе, чтобы задать удобочитаемость и записи NetworkStream.Provide the appropriate FileAccess enumerated value in the constructor to set the readability and writability of the NetworkStream. Свойство CanRead задается при инициализации NetworkStream.The CanRead property is set when the NetworkStream is initialized.

Применяется к

Дополнительно