TcpClient.GetStream メソッド
定義
重要
一部の情報は、リリース前に大きく変更される可能性があるプレリリースされた製品に関するものです。 Microsoft は、ここに記載されている情報について、明示または黙示を問わず、一切保証しません。
データの送受信に使用する NetworkStream を返します。
public:
System::Net::Sockets::NetworkStream ^ GetStream();
public System.Net.Sockets.NetworkStream GetStream ();
member this.GetStream : unit -> System.Net.Sockets.NetworkStream
Public Function GetStream () As NetworkStream
戻り値
基になる NetworkStream。
例外
TcpClient がリモート ホストに接続されていません。
TcpClient が閉じられました。
例
次のコード例では GetStream
、基になる NetworkStream. 取得後、NetworkStreamそのWriteReadメソッドを使用して送受信します。
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;
}
using TcpClient tcpClient = new TcpClient();
tcpClient.ConnectAsync("contoso.com", 5000);
using NetworkStream netStream = tcpClient.GetStream();
// Send some data to the peer.
byte[] sendBuffer = Encoding.UTF8.GetBytes("Is anybody there?");
netStream.Write(sendBuffer);
// Receive some data from the peer.
byte[] receiveBuffer = new byte[1024];
int bytesReceived = netStream.Read(receiveBuffer);
string data = Encoding.UTF8.GetString(receiveBuffer.AsSpan(0, bytesReceived));
Console.WriteLine($"This is what the peer sent to you: {data}");
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
注釈
このメソッドは GetStream
、 NetworkStream データの送受信に使用できる a を返します。 クラスは NetworkStream
、ネットワーク通信を Stream 容易にするために使用されるメソッドとプロパティの豊富なコレクションを提供するクラスから継承します。
最初にメソッドを Connect 呼び出す必要があります。または、メソッドは GetStream InvalidOperationException. 取得した後、リモート ホストに NetworkStream
データを Write 送信するメソッドを呼び出します。 Readリモート ホストから到着したデータを受信するメソッドを呼び出します。 これらのメソッドはどちらも、指定した操作が実行されるまでブロックします。 プロパティを確認することで、読み取り操作でブロックを DataAvailable 回避できます。 値は true
、データがリモート ホストから到着し、読み取り可能であることを意味します。 この場合、 Read すぐに完了することが保証されます。 リモート ホストが接続をシャットダウンした場合、 Read 直ちに 0 バイトで返されます。
注意
を受け取った場合は SocketException、特定のエラー コードを取得するために使用 SocketException.ErrorCode します。 このコードを取得したら、エラーの詳細な説明については、Windows Sockets バージョン 2 API のエラー コードドキュメントを参照してください。
注意
このメンバーは、アプリケーションでネットワーク トレースが有効にされている場合にトレース情報を出力します。 詳細については、「.NET Frameworkのネットワーク トレース」を参照してください。