Export (0) Print
Expand All
2 out of 9 rated this helpful - Rate this topic

TcpClient.GetStream Method

Returns the NetworkStream used to send and receive data.

Namespace:  System.Net.Sockets
Assembly:  System (in System.dll)
public NetworkStream GetStream()

Return Value

Type: System.Net.Sockets.NetworkStream
The underlying NetworkStream.
ExceptionCondition
InvalidOperationException

The TcpClient is not connected to a remote host.

ObjectDisposedException

The TcpClient has been closed.

GetStream returns a NetworkStream that you can use to send and receive data. The NetworkStream class inherits from the Stream class, which provides a rich collection of methods and properties used to facilitate network communications.

You must call the Connect method first, or the GetStream method will throw an InvalidOperationException. After you have obtained the NetworkStream, call the Write method to send data to the remote host. Call the Read method to receive data arriving from the remote host. Both of these methods block until the specified operation is performed. You can avoid blocking on a read operation by checking the DataAvailable property. A true value means that data has arrived from the remote host and is available for reading. In this case, Read is guaranteed to complete immediately. If the remote host has shutdown its connection, Read will immediately return with zero bytes.

NoteNote

You must close the NetworkStream when you are through sending and receiving data. Closing TcpClient does not release the NetworkStream.

NoteNote

If you receive a SocketException, use SocketException.ErrorCode to obtain the specific error code. After you have obtained this code, you can refer to the Windows Sockets version 2 API error code documentation in MSDN for a detailed description of the error.

NoteNote

This member outputs trace information when you enable network tracing in your application. For more information, see Network Tracing in the .NET Framework.

The following code example uses GetStream to obtain the underlying NetworkStream. After obtaining the NetworkStream, it sends and receives using its Write and Read methods.

        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();

.NET Framework

Supported in: 4.5.1, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.