次の方法で共有


NetworkStream.CanRead プロパティ

NetworkStream が読み取りをサポートしているかどうかを示す値を取得します。

Overrides Public ReadOnly Property CanRead As Boolean
[C#]
public override bool CanRead {get;}
[C++]
public: __property bool get_CanRead();
[JScript]
public override function get CanRead() : Boolean;

プロパティ値

ストリームからデータを読み取ることができる場合は true 。それ以外の場合は false 。既定値は true です。

解説

CanReadtrue の場合は、 NetworkStreamRead メソッドを呼び出すことができます。適切な FileAccess 列挙値をコンストラクタで指定して、 NetworkStream の読み取り機能と書き込み機能を設定します。 CanRead プロパティは、 NetworkStream が初期化されたときに設定されます。

使用例

[Visual Basic, C#, C++] CanRead を調べて、 NetworkStream が読み取り可能かどうかを確認する例を次に示します。確認後、 NetworkStream で読み取り操作を実行しています。

 
' 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] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, .NET Compact Framework - Windows CE .NET, Common Language Infrastructure (CLI) Standard

参照

NetworkStream クラス | NetworkStream メンバ | System.Net.Sockets 名前空間 | Readable | FileAccess