System.Net.Security Namespace
Writes data to the stream. This method is asynchronous.
byte array that contains the data to write to the stream.
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.
ArgumentNullException: buffer is null.ArgumentOutOfRangeException: offset or count are negative, or offset + count is greater than the length of buffer.ObjectDisposedException: The stream has been closed.NotSupportedException: The stream does not support writing.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.
SslStream is authenticated before calling Write or WriteAsync.
Write method will write as many bytes as possible before returning.
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!");
// }
}
mscorlib.dll, System.dll
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