Hinweis
Für den Zugriff auf diese Seite ist eine Autorisierung erforderlich. Sie können versuchen, sich anzumelden oder das Verzeichnis zu wechseln.
Für den Zugriff auf diese Seite ist eine Autorisierung erforderlich. Sie können versuchen, das Verzeichnis zu wechseln.
Gibt den für das Senden und Empfangen von Daten verwendeten NetworkStream zurück.
Namespace: System.Net.Sockets
Assembly: System (in system.dll)
Syntax
'Declaration
Public Function GetStream As NetworkStream
'Usage
Dim instance As TcpClient
Dim returnValue As NetworkStream
returnValue = instance.GetStream
public NetworkStream GetStream ()
public:
NetworkStream^ GetStream ()
public NetworkStream GetStream ()
public function GetStream () : NetworkStream
Rückgabewert
Der zugrunde liegende NetworkStream.
Ausnahmen
| Ausnahmetyp | Bedingung |
|---|---|
Der TcpClient ist mit keinem Remotehost verbunden. |
|
Der TcpClient wurde geschlossen. |
Hinweise
GetStream gibt einen NetworkStream zurück, mit dem Sie Daten senden und empfangen können. Die NetworkStream-Klasse erbt von der Stream-Klasse, die eine umfassende Auflistung von Methoden und Eigenschaften für die Netzwerkkommunikation enthält.
Zuerst muss die Connect-Methode aufgerufen werden, sonst löst die GetStream-Methode eine InvalidOperationException aus. Nachdem Sie den NetworkStream abgerufen haben, können Sie durch Aufruf der Write-Methode Daten an den Remotehost senden. Rufen Sie die Read-Methode auf, um Daten zu empfangen, die vom Remotehost eintreffen. Beide Methoden blockieren, bis der angegebene Vorgang ausgeführt sind. Sie können das Blockieren eines Lesevorgangs durch Überprüfen der DataAvailable-Eigenschaft vermeiden. Der Wert true gibt an, dass Daten vom Remotehost empfangen wurden und zum Lesen zur Verfügung stehen. In diesem Fall ist garantiert, dass Read sofort beendet wird. Wenn der Remotehost die Verbindung beendet hat, wird Read sofort beendet und gibt 0 (null) Bytes zurück.
Hinweis
NetworkStream muss nach dem Senden und Empfangen von Daten geschlossen werden. Beim Schließen von TcpClient wird NetworkStream nicht freigegeben.
Hinweis
Wenn Sie eine SocketException erhalten, können Sie mit SocketException.ErrorCode den spezifischen Fehlercode abrufen. Nachdem Sie diesen Code abgerufen haben, finden Sie in MSDN in der Dokumentation zu API-Fehlercodes unter Windows Sockets, Version 2, eine ausführliche Beschreibung des Fehlers.
Hinweis
Dieser Member gibt Ablaufverfolgungsinformationen aus, wenn Sie die Netzwerkablaufverfolgung in der Anwendung aktivieren. Weitere Informationen finden Sie unter Netzwerkablaufverfolgung.
Beispiel
Im folgenden Codebeispiel wird mit GetStream der zugrunde liegende NetworkStream abgerufen. Nach dem Abrufen des NetworkStream wird mit der Write-Methode und der Read-Methode gesendet und empfangen.
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 'MyTcpClientCommunicator
TcpClient tcpClient = new TcpClient ();
// Uses the GetStream public method to return the NetworkStream.
NetworkStream netStream = tcpClient.GetStream ();
if (netStream.CanWrite)
{
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.
byte[] bytes = new 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: " + 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;
}
netStream.Close();
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;
}
TcpClient tcpClient = new TcpClient();
// Uses the GetStream public method to return the NetworkStream.
NetworkStream netStream = tcpClient.GetStream();
if (netStream.get_CanWrite()) {
ubyte sendBytes[] =
Encoding.get_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.get_CanRead()) {
// Reads NetworkStream into a byte buffer.
ubyte bytes[] = new ubyte[tcpClient.get_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.get_ReceiveBufferSize());
// Returns the data received from the host to the console.
String returnData = Encoding.get_UTF8().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;
}
Plattformen
Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile für Pocket PC, Windows Mobile für Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
.NET Framework unterstützt nicht alle Versionen sämtlicher Plattformen. Eine Liste der unterstützten Versionen finden Sie unter Systemanforderungen.
Versionsinformationen
.NET Framework
Unterstützt in: 2.0, 1.1, 1.0
.NET Compact Framework
Unterstützt in: 2.0, 1.0
Siehe auch
Referenz
TcpClient-Klasse
TcpClient-Member
System.Net.Sockets-Namespace
NetworkStream-Klasse
Write
Read
NetworkStream.DataAvailable-Eigenschaft
Stream
Connect