TcpClient.GetStream メソッドとは? わかりやすく解説

Weblio 辞書 > コンピュータ > .NET Framework クラス ライブラリ リファレンス > TcpClient.GetStream メソッドの意味・解説 

TcpClient.GetStream メソッド

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

名前空間: System.Net.Sockets
アセンブリ: System (system.dll 内)
構文構文

Public Function GetStream As
 NetworkStream
Dim instance As TcpClient
Dim returnValue As NetworkStream

returnValue = instance.GetStream
public NetworkStream GetStream ()
public:
NetworkStream^ GetStream ()
public NetworkStream GetStream ()
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 は解放されません。

使用例使用例

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
 
   ' Uses the Close public method to close the network stream and socket.
   tcpClient.Close()
End Sub 'MyTcpClientCommunicator
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;
}
netStream.Close();

TcpClient^ tcpClient = gcnew TcpClient;

// Uses the GetStream public method to return the NetworkStream.
NetworkStream^ netStream = tcpClient->GetStream();
if ( netStream->CanWrite )
{
   array<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.
   array<Byte>^bytes = gcnew array<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: {0}", 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;
}


TcpClient tcpClient = new TcpClient();

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

if (netStream.get_CanWrite()) {
    ubyte sendBytes[] =
        Encoding.get_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.get_CanRead()) {
    // Reads NetworkStream into a byte buffer.
    ubyte bytes[] = new ubyte[tcpClient.get_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.get_ReceiveBufferSize());

    // Returns the data received from the host to the console.
    String returnData = Encoding.get_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;
}
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
TcpClient クラス
TcpClient メンバ
System.Net.Sockets 名前空間
NetworkStream クラス
Write
Read
NetworkStream.DataAvailable プロパティ
Stream
Connect



英和和英テキスト翻訳

英語⇒日本語日本語⇒英語

辞書ショートカット

すべての辞書の索引

「TcpClient.GetStream メソッド」の関連用語

TcpClient.GetStream メソッドのお隣キーワード

   

英語⇒日本語
日本語⇒英語
   



TcpClient.GetStream メソッドのページの著作権

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2026 Microsoft.All rights reserved.

©2026 GRAS Group, Inc.RSS