TcpClient.GetStream Yöntem

Tanım

Veri göndermek ve almak için kullanılan değerini NetworkStream döndürür.

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

Döndürülenler

NetworkStream

Temel alınan NetworkStream.

Özel durumlar

TcpClient uzak bir konağa bağlı değil.

Örnekler

Aşağıdaki kod örneği, temel alınan NetworkStreamöğesini almak için kullanırGetStream. aldıktan NetworkStreamsonra ve yöntemlerini kullanarak Write Read gönderip alır.

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

Açıklamalar

yöntemi, GetStream veri gönderip almak için kullanabileceğiniz bir NetworkStream döndürür. NetworkStream sınıfı, ağ iletişimini Stream kolaylaştırmak için kullanılan yöntem ve özelliklerden oluşan zengin bir koleksiyon sağlayan sınıfından devralır.

Önce yöntemini çağırmanız Connect gerekir, aksi takdirde GetStream yöntemi bir InvalidOperationExceptionoluşturur. elde ettikten NetworkStreamsonra uzak konağa veri göndermek için yöntemini çağırın Write . Read Uzak konaktan gelen verileri almak için yöntemini çağırın. Bu yöntemlerin ikisi de belirtilen işlem gerçekleştirilene kadar engeller. Özelliğini denetleyerek bir okuma işleminde engellemeyi DataAvailable önleyebilirsiniz. Değer true , verilerin uzak konaktan geldiği ve okunabilecek durumda olduğu anlamına gelir. Bu durumda, Read hemen tamamlanması garanti edilir. Uzak ana bilgisayar bağlantısını kapattıysa, Read hemen sıfır bayt ile döner.

Not

bir alırsanız, belirli bir SocketExceptionhata kodunu almak için kullanın SocketException.ErrorCode . Bu kodu aldıktan sonra, hatanın ayrıntılı açıklaması için Windows Sockets sürüm 2 API hata kodu belgelerine bakabilirsiniz.

Not

Uygulamanızda ağ izlemeyi etkinleştirdiğinizde, bu üye izleme bilgilerini çıkarır. Daha fazla bilgi için .NET Framework ağ izleme bölümüne bakın.

Şunlara uygulanır

Ayrıca bkz.