TcpClient.GetStream メソッド
アセンブリ: System (system.dll 内)

Dim instance As TcpClient Dim returnValue As NetworkStream returnValue = instance.GetStream
基になる NetworkStream。


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; }

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


Weblioに収録されているすべての辞書からTcpClient.GetStream メソッドを検索する場合は、下記のリンクをクリックしてください。

- TcpClient.GetStream メソッドのページへのリンク