Export (0) Print
Expand All

NetworkStream Constructor (Socket, FileAccess)

Creates a new instance of the NetworkStream class for the specified Socket with the specified access rights.

Namespace:  System.Net.Sockets
Assembly:  System (in System.dll)

public NetworkStream(
	Socket socket,
	FileAccess access


Type: System.Net.Sockets.Socket

The Socket that the NetworkStream will use to send and receive data.

Type: System.IO.FileAccess

A bitwise combination of the FileAccess values that specify the type of access given to the NetworkStream over the provided Socket.


The socket parameter is null.


The socket parameter is not connected.


the SocketType property of the socket parameter is not SocketType.Stream.


the socket parameter is in a nonblocking state.

The NetworkStream is created with the specified access to the specified Socket. With this constructor, the NetworkStream does not own the underlying Socket, so calling the Close method does not close the underlying Socket.

The access parameter sets the CanRead and CanWrite properties of the NetworkStream. If you specify Write, then the NetworkStream allows calls to the Write method. If you specify Read, then the NetworkStream allows calls to the Read method. If you specify ReadWrite, both method calls are allowed.

The following code example creates a NetworkStream with the ability to read and write to the Socket.

        // Example for creating a NetworkStreams


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

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

.NET Framework

Supported in: 4.6, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

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.

© 2014 Microsoft