Bagikan melalui


TcpClient.GetStream Metode

Definisi

Mengembalikan data yang NetworkStream digunakan untuk mengirim dan menerima data.

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

Mengembalikan

Yang mendasar NetworkStream.

Pengecualian

TcpClient tidak tersambung ke host jarak jauh.

Contoh

Contoh kode berikut menggunakan untuk mendapatkan yang mendasar GetStreamNetworkStream. Setelah mendapatkan NetworkStream, ia mengirim dan menerima menggunakan Write metode dan 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

Keterangan

Metode GetStream mengembalikan NetworkStream yang dapat Anda gunakan untuk mengirim dan menerima data. Kelas NetworkStream mewarisi dari Stream kelas , yang menyediakan koleksi metode dan properti yang kaya yang digunakan untuk memfasilitasi komunikasi jaringan.

Anda harus memanggil metode terlebih Connect dahulu, atau GetStream metode akan melemparkan InvalidOperationException. Setelah Anda mendapatkan NetworkStream, panggil Write metode untuk mengirim data ke host jarak jauh. Read Panggil metode untuk menerima data yang tiba dari host jarak jauh. Kedua metode ini memblokir hingga operasi yang ditentukan dilakukan. Anda dapat menghindari pemblokiran pada operasi baca dengan memeriksa DataAvailable properti . Nilai true berarti bahwa data telah tiba dari host jarak jauh dan tersedia untuk dibaca. Dalam hal ini, Read dijamin akan segera selesai. Jika host jarak jauh telah mematikan koneksinya, Read akan segera kembali dengan nol byte.

Catatan

Jika Anda menerima SocketException, gunakan SocketException.ErrorCode untuk mendapatkan kode kesalahan tertentu. Setelah mendapatkan kode ini, Anda dapat merujuk ke dokumentasi kode kesalahan API Windows Sockets versi 2 untuk deskripsi terperinci tentang kesalahan tersebut.

Catatan

Anggota ini mengeluarkan informasi pelacakan saat Anda mengaktifkan pelacakan jaringan di aplikasi Anda. Untuk informasi selengkapnya, lihat Pelacakan Jaringan di .NET Framework.

Berlaku untuk

Lihat juga