This documentation is archived and is not being maintained.

SslStream.Write Method (Byte[])

Writes the specified data to this stream.

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

public void Write(
	byte[] buffer
)

Parameters

buffer
Type: System.Byte[]

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

ExceptionCondition
ArgumentNullException

buffer is null.

IOException

The write operation failed.

NotSupportedException

There is already a write operation in progress.

ObjectDisposedException

This object has been closed.

InvalidOperationException

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 = 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();
    }
}
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->ServerAuthenticate(serverCertificate, 
            false, SslProtocolType::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(S"Waiting for client message...");
        String* messageData = ReadMessage(sslStream);
        Console::WriteLine(S"Received: {0}", messageData);

        // Write a message to the client.
        Byte message[] = Encoding::UTF8->GetBytes(S"Hello from the server.<EOF>");
        Console::WriteLine(S"Sending hello message.");
        sslStream->Write(message);
    }
    catch (AuthenticationException* e)
    {
        Console::WriteLine(S"Exception: {0}", e->Message);
        if (e->InnerException != 0)
        {
            Console::WriteLine(S"Inner exception: {0}", e->InnerException->Message);
        }
        Console::WriteLine (S"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 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

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

.NET Framework

Supported in: 3.5, 3.0, 2.0
Show: