次の方法で共有


TcpClient.GetStream メソッド

データの送受信に使用する NetworkStream を返します。

Public Function GetStream() As NetworkStream
[C#]
public NetworkStream GetStream();
[C++]
public: NetworkStream* GetStream();
[JScript]
public function GetStream() : NetworkStream;

戻り値

基になる NetworkStream

例外

例外の種類 条件
InvalidOperationException TcpClient がリモート ホストに接続されていません。
ObjectDisposedException TcpClient が閉じられています。

解説

GetStream は、データの送受信に使用できる NetworkStream を返します。 NetworkStream クラスは、ネットワーク通信の簡略化に使用される、メソッドとプロパティの豊富なコレクションを提供する Stream クラスから継承されます。

まず、 Connect メソッドを呼び出す必要があります。このメソッドを呼び出さないと、 GetStream メソッドは InvalidOperationException をスローします。 NetworkStream を取得したら、 Write メソッドを呼び出して、リモート ホストにデータを送信します。 Read メソッドを呼び出して、リモート ホストからデータを受信します。これらのメソッドは両方とも、指定した操作が実行されるまでブロックします。ただし、読み取り操作の場合は、 DataAvailable プロパティをチェックすることでブロックを回避できます。値 true は、リモート ホストからのデータが到達し、読み取ることができるようになっていることを意味します。このような場合、 Read はすぐに完了します。リモート ホストが接続をシャットダウンした場合は、 Read がすぐに終了し、ゼロ バイトが返されます。

メモ   データの送受信を完了したときは、 NetworkStream を終了する必要があります。 TcpClient を終了しても、 NetworkStream は解放されません。

メモ    SocketException が発生した場合は、 SocketException.ErrorCode を使用して具体的なエラー コードを取得してください。このコードを取得したら、Windows Socket Version 2 API エラー コードのマニュアルから、エラーの詳細情報を確認できます。これは MSDN から入手できます。

使用例

[Visual Basic, C#, C++] GetStream を使用して基になる NetworkStream を取得する例を次に示します。 NetworkStream を取得した後、その Write メソッドと Read メソッドを使用して送受信します。

 
Dim tcpClient As New TcpClient()
' Uses the GetStream public method to return the NetworkStream.

   Dim netStream As NetworkStream = tcpClient.GetStream()
   If netStream.CanWrite Then
      Dim sendBytes As [Byte]() = Encoding.UTF8.GetBytes("Is anybody there?")
      netStream.Write(sendBytes, 0, sendBytes.Length)
   Else
      Console.WriteLine("You cannot write data to this stream.")
      tcpClient.Close()
      ' Closing the tcpClient instance does not close the network stream.
      netStream.Close()
      Return
   End If
   If netStream.CanRead Then
      
      ' Reads the NetworkStream into a byte buffer.
      Dim bytes(tcpClient.ReceiveBufferSize) As Byte
      ' Read can return anything from 0 to numBytesToRead. 
      ' This method blocks until at least one byte is read.
      netStream.Read(bytes, 0, CInt(tcpClient.ReceiveBufferSize))
      
      ' Returns the data received from the host to the console.
      Dim returndata As String = Encoding.ASCII.GetString(bytes)
      Console.WriteLine(("This is what the host returned to you: " + returndata))
   Else
      Console.WriteLine("You cannot read data from this stream.")
      tcpClient.Close()
      ' Closing the tcpClient instance does not close the network stream.
      netStream.Close()
      Return
   End If
 

[C#] 
TcpClient tcpClient = new TcpClient ();

// Uses the GetStream public method to return the NetworkStream.
NetworkStream netStream = tcpClient.GetStream ();

if (netStream.CanWrite)
{
    Byte[] sendBytes = Encoding.UTF8.GetBytes ("Is anybody there?");
    netStream.Write (sendBytes, 0, sendBytes.Length);
}
else
{
    Console.WriteLine ("You cannot write data to this stream.");
    tcpClient.Close ();

    // Closing the tcpClient instance does not close the network stream.
    netStream.Close ();
    return;
}

if (netStream.CanRead)
{
    // Reads NetworkStream into a byte buffer.
    byte[] bytes = new byte[tcpClient.ReceiveBufferSize];

    // Read can return anything from 0 to numBytesToRead. 
    // This method blocks until at least one byte is read.
    netStream.Read (bytes, 0, (int)tcpClient.ReceiveBufferSize);

    // Returns the data received from the host to the console.
    string returndata = Encoding.UTF8.GetString (bytes);

    Console.WriteLine ("This is what the host returned to you: " + returndata);
}
else
{
    Console.WriteLine ("You cannot read data from this stream.");
    tcpClient.Close ();

    // Closing the tcpClient instance does not close the network stream.
    netStream.Close ();
    return;
}


[C++] 
TcpClient* tcpClient = new TcpClient();
// Uses the GetStream public method to return the NetworkStream.

    NetworkStream* netStream = tcpClient->GetStream();
    if(netStream->CanWrite)
    {
        Byte sendBytes[] = Encoding::UTF8->GetBytes(S"Is anybody there?");
        netStream->Write(sendBytes, 0, sendBytes->Length);
    }
    else
    {
        Console::WriteLine(S"You cannot write data to this stream.");
        tcpClient->Close();
        
        // Closing the tcpClient instance does not close the network stream.
        netStream->Close ();
        return;
    }
    if(netStream->CanRead){
     
      // Reads NetworkStream into a byte buffer.
      Byte bytes[] = new Byte[tcpClient->ReceiveBufferSize];
      // Read can return anything from 0 to numBytesToRead. 
      // This method blocks until at least one byte is read.
      netStream->Read(bytes, 0, (int) tcpClient->ReceiveBufferSize);
             
     // Returns the data received from the host to the console.
     String* returndata = Encoding::UTF8->GetString(bytes);
     Console::WriteLine(S"This is what the host returned to you: {0}", returndata);
    }
    else
    {
         Console::WriteLine(S"You cannot read data from this stream.");
         tcpClient->Close();
         
         // Closing the tcpClient instance does not close the network stream.
         netStream->Close ();
         return;
    }

  

[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

参照

TcpClient クラス | TcpClient メンバ | System.Net.Sockets 名前空間 | NetworkStream | Write | Read | DataAvailable | Stream | Connect