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


このページでは「.NET Framework クラス ライブラリ リファレンス」からTcpClient.GetStream メソッドを検索した結果を表示しています。
Weblioに収録されているすべての辞書からTcpClient.GetStream メソッドを検索する場合は、下記のリンクをクリックしてください。
 全ての辞書からTcpClient.GetStream メソッド を検索

英和和英テキスト翻訳>> Weblio翻訳
英語⇒日本語日本語⇒英語
  

辞書ショートカット

すべての辞書の索引

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

TcpClient.GetStream メソッドのお隣キーワード
検索ランキング

   

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



TcpClient.GetStream メソッドのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

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

©2025 GRAS Group, Inc.RSS