TcpClient.GetStream 方法

定义

返回用于发送和接收数据的 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 未连接到远程主机。

示例

下面的代码示例使用 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 可用于发送和接收数据的 。 类 NetworkStream 继承自 Stream 类,该类提供用于促进网络通信的方法和属性的丰富集合。

必须首先调用 Connect 方法,否则该方法 GetStream 将引发 InvalidOperationException。 获取 后, NetworkStream调用 Write 方法将数据发送到远程主机。 Read调用 方法以接收来自远程主机的数据。 在执行指定的操作之前,这两种方法均会阻塞。 可以通过检查 DataAvailable 属性来避免阻塞读取操作。 值 true 表示数据已从远程主机到达并可供读取。 在这种情况下, Read 保证立即完成。 如果远程主机关闭了其连接, Read 将立即返回 0 个字节。

注意

如果收到 SocketException,请使用 SocketException.ErrorCode 获取特定的错误代码。 获取此代码后,可以参阅 Windows 套接字版本 2 API 错误代码 文档,获取错误的详细说明。

备注

当你在应用程序中启用网络跟踪后,此成员将输出跟踪信息。 有关详细信息,请参阅.NET Framework中的网络跟踪

适用于

另请参阅