Export (0) Print
Expand All

NetworkStream Constructor (Socket)

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.


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

Supported in: 4.5.2, 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 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