SslStream Constructor (Stream^, Boolean)
Initializes a new instance of the SslStream class using the specified Stream and stream closure behavior.
Assembly: System (in System.dll)
Parameters
- innerStream
-
Type:
System.IO::Stream^
A Stream object used by the SslStream for sending and receiving data.
- leaveInnerStreamOpen
-
Type:
System::Boolean
A Boolean value that indicates the closure behavior of the Stream object used by the SslStream for sending and receiving data. This parameter indicates if the inner stream is left open.
| Exception | Condition |
|---|---|
| ArgumentException | innerStream is not readable. -or- innerStream is not writable. |
| ArgumentNullException |
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.
If a value is not specified in the configuration file for encryptionpolicy, the EncryptionPolicy defaults to EncryptionPolicy::RequireEncryption for the SslStream instance that is constructed.
The use of the Null cipher is required when the encryption policy is set to EncryptionPolicy::NoEncryption.
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 = gcnew 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( L"Waiting for client message..." ); String^ messageData = ReadMessage( sslStream ); Console::WriteLine( L"Received: {0}", messageData ); // Write a message to the client. array<Byte>^message = Encoding::UTF8->GetBytes( L"Hello from the server.<EOF>" ); Console::WriteLine( L"Sending hello message." ); sslStream->Write( message ); } catch ( AuthenticationException^ e ) { Console::WriteLine( L"Exception: {0}", e->Message ); if ( e->InnerException != nullptr ) { Console::WriteLine( L"Inner exception: {0}", e->InnerException->Message ); } Console::WriteLine( L"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(); } }
Available since 2.0