NetworkStream Constructors

Definition

Creates a new instance of the NetworkStream class.

Overloads

NetworkStream(Socket)

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

NetworkStream(Socket, Boolean)

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

NetworkStream(Socket, FileAccess)

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

NetworkStream(Socket, FileAccess, Boolean)

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

NetworkStream(Socket)

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

public:
 NetworkStream(System::Net::Sockets::Socket ^ socket);
public NetworkStream (System.Net.Sockets.Socket socket);
new System.Net.Sockets.NetworkStream : System.Net.Sockets.Socket -> System.Net.Sockets.NetworkStream

Parameters

socket
Socket

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

Exceptions

The socket parameter is null.

The socket parameter is not connected.

-or-

The SocketType property of the socket parameter is not Stream.

-or-

The socket parameter is in a nonblocking state.

Examples

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 = gcnew NetworkStream( mySocket,true );
}
else
{
   myNetworkStream = gcnew NetworkStream( mySocket );
}
// 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);     
}
' Examples for constructors that do not specify file permission.
' Create the NetworkStream for communicating with the remote host.
Dim myNetworkStream As NetworkStream

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

Remarks

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.

See also

NetworkStream(Socket, Boolean)

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

public:
 NetworkStream(System::Net::Sockets::Socket ^ socket, bool ownsSocket);
public NetworkStream (System.Net.Sockets.Socket socket, bool ownsSocket);
new System.Net.Sockets.NetworkStream : System.Net.Sockets.Socket * bool -> System.Net.Sockets.NetworkStream

Parameters

socket
Socket

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

ownsSocket
Boolean

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

Exceptions

The socket parameter is null.

The socket parameter is not connected.

-or-

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

-or-

the socket parameter is in a nonblocking state.

Examples

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 = gcnew NetworkStream( mySocket,true );
}
else
{
   myNetworkStream = gcnew NetworkStream( mySocket );
}
// 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);     
}
' Examples for constructors that do not specify file permission.
' Create the NetworkStream for communicating with the remote host.
Dim myNetworkStream As NetworkStream

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

Remarks

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.

See also

NetworkStream(Socket, FileAccess)

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

public:
 NetworkStream(System::Net::Sockets::Socket ^ socket, System::IO::FileAccess access);
public NetworkStream (System.Net.Sockets.Socket socket, System.IO.FileAccess access);
new System.Net.Sockets.NetworkStream : System.Net.Sockets.Socket * System.IO.FileAccess -> System.Net.Sockets.NetworkStream

Parameters

socket
Socket

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

access
FileAccess

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

Exceptions

The socket parameter is null.

The socket parameter is not connected.

-or-

the SocketType property of the socket parameter is not Stream.

-or-

the socket parameter is in a nonblocking state.

Examples

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

// Example for creating a NetworkStreams
mySocket->Connect( myIpEndPoint );

// Create the NetworkStream for communicating with the remote host.
NetworkStream^ myNetworkStream;
if ( networkStreamOwnsSocket )
{
   myNetworkStream = gcnew NetworkStream( mySocket,FileAccess::ReadWrite,true );
}
else
{
   myNetworkStream = gcnew NetworkStream( mySocket,FileAccess::ReadWrite );
}
        // Example for creating a NetworkStreams
        
        mySocket.Connect(myIpEndPoint);
                 
        // Create the NetworkStream for communicating with the remote host.
        NetworkStream myNetworkStream;
        
        if (networkStreamOwnsSocket){
             myNetworkStream = new NetworkStream(mySocket, FileAccess.ReadWrite, true);          
        }
        else{
             myNetworkStream = new NetworkStream(mySocket, FileAccess.ReadWrite);     
        }

' Example for creating a NetworkStreams
mySocket.Connect(myIpEndPoint)

' Create the NetworkStream for communicating with the remote host.
Dim myNetworkStream As NetworkStream

If networkStreamOwnsSocket Then
   myNetworkStream = New NetworkStream(mySocket, FileAccess.ReadWrite, True)
Else
   myNetworkStream = New NetworkStream(mySocket, FileAccess.ReadWrite)
End If

Remarks

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.

See also

NetworkStream(Socket, FileAccess, Boolean)

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

public:
 NetworkStream(System::Net::Sockets::Socket ^ socket, System::IO::FileAccess access, bool ownsSocket);
public NetworkStream (System.Net.Sockets.Socket socket, System.IO.FileAccess access, bool ownsSocket);
new System.Net.Sockets.NetworkStream : System.Net.Sockets.Socket * System.IO.FileAccess * bool -> System.Net.Sockets.NetworkStream

Parameters

socket
Socket

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

access
FileAccess

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

ownsSocket
Boolean

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

Exceptions

The socket parameter is null.

The socket parameter is not connected.

-or-

The SocketType property of the socket parameter is not Stream.

-or-

The socket parameter is in a nonblocking state.

Examples

The following code example creates a NetworkStream with the ability to read and write to the Socket. Ownership of the Socket is given to this NetworkStream by specifying true for the ownsSocket parameter.

// Example for creating a NetworkStreams
mySocket->Connect( myIpEndPoint );

// Create the NetworkStream for communicating with the remote host.
NetworkStream^ myNetworkStream;
if ( networkStreamOwnsSocket )
{
   myNetworkStream = gcnew NetworkStream( mySocket,FileAccess::ReadWrite,true );
}
else
{
   myNetworkStream = gcnew NetworkStream( mySocket,FileAccess::ReadWrite );
}
        // Example for creating a NetworkStreams
        
        mySocket.Connect(myIpEndPoint);
                 
        // Create the NetworkStream for communicating with the remote host.
        NetworkStream myNetworkStream;
        
        if (networkStreamOwnsSocket){
             myNetworkStream = new NetworkStream(mySocket, FileAccess.ReadWrite, true);          
        }
        else{
             myNetworkStream = new NetworkStream(mySocket, FileAccess.ReadWrite);     
        }

' Example for creating a NetworkStreams
mySocket.Connect(myIpEndPoint)

' Create the NetworkStream for communicating with the remote host.
Dim myNetworkStream As NetworkStream

If networkStreamOwnsSocket Then
   myNetworkStream = New NetworkStream(mySocket, FileAccess.ReadWrite, True)
Else
   myNetworkStream = New NetworkStream(mySocket, FileAccess.ReadWrite)
End If

Remarks

The NetworkStream is created with read/write access to the specified Socket. If the value of the ownsSocket parameter is true, the NetworkStream takes ownership of the underlying Socket, and calling the Close method also closes 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.

See also

Applies to