NetworkStream Constructor (Socket, Boolean)

 

Initializes a new instance of the NetworkStream class for the specified Socket with the specified Socket ownership.

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

public NetworkStream(
	Socket socket,
	bool ownsSocket
)

Parameters

socket
Type: System.Net.Sockets.Socket

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

ownsSocket
Type: System.Boolean

Set to true to indicate that the NetworkStream will take ownership of the Socket; otherwise, false.

Exception Condition
ArgumentNullException

The socket parameter is null.

IOException

The socket parameter is not connected.

-or-

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

-or-

the socket parameter is in a nonblocking state.

The NetworkStream is created with read/write access to the specified Socket. If the value of ownsSocket parameter is true, the NetworkStream takes ownership of the underlying Socket, and calling the Close method also closes the underlying Socket.

The following code example creates a NetworkStream with ownership of the 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);          
}
else{
     myNetworkStream = new NetworkStream(mySocket);     
}

.NET Framework
Available since 1.1
Return to top
Show: