Export (0) Print
Expand All

SslStream::Write Method (array<Byte>)

Writes the specified data to this stream.

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

void Write(
	array<unsigned char>^ buffer


Type: array<System::Byte>

A Byte array that supplies the bytes written to the stream.


buffer is nullptr.


The write operation failed.


There is already a write operation in progress.


This object has been closed.


Authentication has not occurred.

This method blocks while the operation completes. To prevent blocking while the operation completes, use the BeginWrite method.

You cannot call this method until you have successfully authenticated. To authenticate call one of the AuthenticateAsClient, or BeginAuthenticateAsClient, AuthenticateAsServer, BeginAuthenticateAsServer methods.

The SslStream class does not support multiple simultaneous write operations.

The following code example demonstrates writing to an authenticated SslStream.

   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. 
         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." );

         // The client stream will be closed with the sslStream 
         // because we specified this behavior when creating 
         // the sslStream.


.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0, 2.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