This documentation is archived and is not being maintained.

NetworkStream Class

Provides the underlying stream of data for network access.

For a list of all members of this type, see NetworkStream Members.


[Visual Basic]
Public Class NetworkStream
   Inherits Stream
public class NetworkStream : Stream
public __gc class NetworkStream : public Stream
public class NetworkStream extends Stream

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.


The NetworkStream class provides methods for sending and receiving data over Stream sockets in blocking mode. For more information of blocking versus non-blocking Socket, see Using an Asynchronous Client Socket.You can use the NetworkStream class for both synchronous and asynchronous data transfer. For more information on asynchronous versus synchronous communication, see Sockets. In order to create a NetworkStream, you must provide a connected Socket. You can also specify what FileAccess permission the NetworkStream has over the provided Socket. By default, closing the NetworkStream does not close the provided Socket. If you want the NetworkStream to have permission to close the provided Socket, you must specify true for the value of the ownsSocket constructor parameter.

Use the Write and Read methods for simple single thread synchronous blocking I/O. If you want to process your I/O using separate threads, consider using the BeginWrite/ EndWrite and BeginRead/ EndRead methods for communication.

The NetworkStream does not support random access to the network data stream. The value of the CanSeek property, which indicates whether the stream supports seeking, is always false; reading the Position property, reading the Length property, or calling the Seek method will throw a NotSupportedException.


[Visual Basic, C#, C++] The following example demonstrates how to create a NetworkStream from a connected Stream Socket and perform basic synchronous blocking I/O.

[Visual Basic] 
' Create the NetworkStream for communicating with the remote host.
Dim myNetworkStream As NetworkStream

If networkStreamOwnsSocket Then
   myNetworkStream = New NetworkStream(mySocket, True)
   myNetworkStream = New NetworkStream(mySocket)
End If


// Create the NetworkStream for communicating with the remote host.
NetworkStream myNetworkStream;

if (networkStreamOwnsSocket){
     myNetworkStream = new NetworkStream(mySocket, true);          
     myNetworkStream = new NetworkStream(mySocket);     


        // Create the NetworkStream for communicating with the remote host.
        NetworkStream* myNetworkStream;

        if (networkStreamOwnsSocket) {
            myNetworkStream = new NetworkStream(mySocket, true);          
        } else {
            myNetworkStream = new NetworkStream(mySocket);     

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.


Namespace: System.Net.Sockets

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

Assembly: System (in System.dll)

See Also

NetworkStream Members | System.Net.Sockets Namespace | TcpClient