NetworkStream Constructor (Socket)


The .NET API Reference documentation has a new home. Visit the .NET API Browser on to see the new experience.

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

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

public NetworkStream(
	Socket socket


Type: System.Net.Sockets.Socket

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

Exception Condition

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 read/write access to the specified Socket. The NetworkStream does not own the underlying Socket, so calling the Close method does not close the Socket.

The following code example illustrates how to create a NetworkStream with a Socket.

// Examples for constructors that do not specify file permission.

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

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

.NET Framework
Available since 1.1
Return to top