Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
SslStream Constructor (Stream, Boolean)

SslStream Constructor (Stream, Boolean)

Note: This constructor is new in the .NET Framework version 2.0.

Initializes a new instance of the SslStream class using the specified Stream and stream closure behavior.

Namespace: System.Net.Security
Assembly: System (in system.dll)

public SslStream (
	Stream innerStream,
	bool leaveInnerStreamOpen
)
public SslStream (
	Stream innerStream, 
	boolean leaveInnerStreamOpen
)
public function SslStream (
	innerStream : Stream, 
	leaveInnerStreamOpen : boolean
)

Parameters

innerStream

Initializes a new instance of the SslStream class using the specified Stream and stream closure behavior.

leaveInnerStreamOpen

Initializes a new instance of the SslStream class using the specified Stream and stream closure behavior.

Exception typeCondition

ArgumentException

innerStream is not readable.

-or-

innerStream is not writable.

ArgumentNullException

innerStream is a null reference (Nothing in Visual Basic).

-or-

innerStream is equal to Null.

When you specify true for the leaveStreamOpen parameter, closing the SslStream has no effect on the innerStream stream; you must explicitly close innerStream when you no longer need it.

The following code example demonstrates calling this constructor.

static void ProcessClient (TcpClient client)
{
    // A client has connected. Create the 
    // SslStream using the client's network stream.
    SslStream sslStream = new SslStream(
        client.GetStream(), false);
    // Authenticate the server but don't require the client to authenticate.
    try 
    {
        sslStream.AuthenticateAsServer(serverCertificate, 
            false, SslProtocols.Tls, true);
        // Display the properties and settings for the authenticated stream.
        DisplaySecurityLevel(sslStream);
        DisplaySecurityServices(sslStream);
        DisplayCertificateInformation(sslStream);
        DisplayStreamProperties(sslStream);

        // Set timeouts for the read and write to 5 seconds.
        sslStream.ReadTimeout = 5000;
        sslStream.WriteTimeout = 5000;
        // Read a message from the client.   
        Console.WriteLine("Waiting for client message...");
        string messageData = ReadMessage(sslStream);
        Console.WriteLine("Received: {0}", messageData);
        
        // Write a message to the client.
        byte[] message = Encoding.UTF8.GetBytes("Hello from the server.<EOF>");
        Console.WriteLine("Sending hello message.");
        sslStream.Write(message);
    }
    catch (AuthenticationException e)
    {
        Console.WriteLine("Exception: {0}", e.Message);
        if (e.InnerException != null)
        {
            Console.WriteLine("Inner exception: {0}", e.InnerException.Message);
        }
        Console.WriteLine ("Authentication failed - closing the connection.");
        sslStream.Close();
        client.Close();
        return;
    }
    finally
    {
        // The client stream will be closed with the sslStream
        // because we specified this behavior when creating
        // the sslStream.
        sslStream.Close();
        client.Close();
    }
}

Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0

Community Additions

ADD
Show:
© 2015 Microsoft