TcpClient.GetStream Metoda

Definice

Vrátí hodnotu použitou NetworkStream k odesílání a přijímání dat.

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

Návraty

Základní NetworkStreamhodnota .

Výjimky

Není TcpClient připojen ke vzdálenému hostiteli.

Příklady

Následující příklad kódu používá GetStream k získání podkladového NetworkStreamobjektu . Po získání NetworkStreamodešle a přijme metodu Write a Read .

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

Poznámky

Metoda GetStream vrátí NetworkStream hodnotu, kterou můžete použít k odesílání a přijímání dat. Třída NetworkStream dědí z Stream třídy, která poskytuje bohatou kolekci metod a vlastností používaných k usnadnění síťové komunikace.

Musíte nejprve volat metodu ConnectGetStream , jinak metoda vyvolá InvalidOperationException. Po získání NetworkStreammetody volejte metodu Write pro odeslání dat vzdálenému hostiteli. Zavolejte metodu Read pro příjem dat přicházejících ze vzdáleného hostitele. Obě tyto metody blokují, dokud se neprovede zadaná operace. Blokování operace čtení se můžete vyhnout kontrolou DataAvailable vlastnosti. Hodnota true znamená, že data přišla ze vzdáleného hostitele a jsou k dispozici pro čtení. V takovém případě je zaručeno, Read že se dokončí okamžitě. Pokud vzdálený hostitel ukončil připojení, Read okamžitě se vrátí s nulovými bajty.

Poznámka

Pokud se zobrazí SocketException, použijte SocketException.ErrorCode k získání konkrétního kódu chyby. Jakmile tento kód získáte, můžete si projděte podrobný popis chyby v dokumentaci k rozhraní API windows Sockets verze 2 .

Poznámka

Tento člen poskytuje trasovací informace, když je ve vaší aplikaci povoleno trasování sítě. Další informace najdete v tématu Trasování sítě v rozhraní .NET Framework.

Platí pro

Viz také