SslStream.Write Method

Summary

Writes data to the stream. This method is asynchronous.

Syntax

public override int Write(byte[] buffer, int offset, int count)
public override async Task<int> WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)

Parameters

byte[] buffer
A byte array that contains the data to write to the stream.
int offset
The zero-based position in the array from which to start copying data.
int count
The number of bytes to write from the buffer.
CancellationToken cancellationToken
A token that can be used to request cancellation of the asynchronous operation.

Return Value

The total number of bytes written to the stream. This can be less than the number of bytes requested if the stream is not able to accommodate the request at the current time.

Exceptions

Remarks

The Write method writes the specified number of bytes from the byte array to the current stream and advances the current position within the stream by the number of bytes written.

The WriteAsync method performs the same operations as the Write method but allows the caller to cancel the operation and to defer the work to another thread.

Important: You must ensure that the SslStream is authenticated before calling Write or WriteAsync.
Note: If the stream is unable to accept all the bytes in the requested operation, the Write method will write as many bytes as possible before returning.

Examples

The following code example demonstrates how to write data to an SslStream.


using System;
using System.Net.Security;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;

public class SslWriteStreamExample
{
    public static async Task WriteToSslStream(SslStream sslStream, string message)
    {
        try
        {
            byte[] messageBytes = Encoding.UTF8.GetBytes(message);
            await sslStream.WriteAsync(messageBytes, 0, messageBytes.Length);
            await sslStream.FlushAsync();
            Console.WriteLine($"Successfully wrote: {message}");
        }
        catch (Exception ex)
        {
            Console.WriteLine($"Error writing to SSL stream: {ex.Message}");
        }
    }

    // Example usage (assuming sslStream is already established and authenticated)
    // public static async Task Main(string[] args)
    // {
    //     // ... obtain and authenticate sslStream ...
    //     // SslStream sslStream = ...;
    //     // await WriteToSslStream(sslStream, "Hello, secure world!");
    // }
}
            

Requirements

Assembly

mscorlib.dll, System.dll

Namespace

System.Net.Security

Assembly and File Information

    Platform: .NET Framework 2.0, .NET Framework 3.5, .NET Framework 4.0, .NET Framework 4.5, .NET Framework 4.6, .NET Framework 4.7, .NET Framework 4.8, .NET Core 1.0, .NET Core 1.1, .NET Core 2.0, .NET Core 2.1, .NET Core 2.2, .NET Standard 1.3, .NET Standard 1.4, .NET Standard 1.5, .NET Standard 1.6, .NET Standard 2.0, .NET 5, .NET 6, .NET 7
    Mscorlib.dll, System.dll