SslStream.Write Method

Definition

Writes data to this stream.

Overloads

Write(Byte[])

Writes the specified data to this stream.

Write(Byte[], Int32, Int32)

Write the specified number of Bytes to the underlying stream using the specified buffer and offset.

Write(Byte[])

Source:
SslStream.cs
Source:
SslStream.cs
Source:
SslStream.cs

Writes the specified data to this stream.

C#
public void Write(byte[] buffer);

Parameters

buffer
Byte[]

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

Exceptions

buffer is null.

The write operation failed.

There is already a write operation in progress.

This object has been closed.

Authentication has not occurred.

Examples

The following code example demonstrates writing to an authenticated SslStream.

C#
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, clientCertificateRequired: false, checkCertificateRevocation: 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();
    }
}

Remarks

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.

Applies to

.NET 10 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

Write(Byte[], Int32, Int32)

Source:
SslStream.cs
Source:
SslStream.cs
Source:
SslStream.cs

Write the specified number of Bytes to the underlying stream using the specified buffer and offset.

C#
public override void Write(byte[] buffer, int offset, int count);

Parameters

buffer
Byte[]

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

offset
Int32

A Int32 that contains the zero-based location in buffer at which to begin reading bytes to be written to the stream.

count
Int32

A Int32 that contains the number of bytes to read from buffer.

Exceptions

buffer is null.

offset is less than zero.

-or-

offset is greater than the length of buffer.

-or-

offset + count is greater than the length of buffer.

The write operation failed.

There is already a write operation in progress.

This object has been closed.

Authentication has not occurred.

Remarks

This method blocks while the operation completes. To prevent blocking while the operation completes 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.

Applies to

.NET 10 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1