SslStream.BeginWrite Method
Table of Contents
Syntax
public IAsyncResult BeginWrite(
byte[] buffer,
int offset,
int count,
AsyncCallback callback,
object state
)
Note: This method is asynchronous. To call this method in Visual Basic and C#, put the contents of the method in a subroutine or function and then call the BeginWrite method. Then pass `AsyncCallback` from the subroutine or function to the `BeginWrite` method.
Parameters
| Parameter | Description |
|---|---|
buffer |
A byte array that contains the data to write to the stream. |
offset |
The zero-based position in the buffer from which to begin copying bytes to the current stream. |
count |
The number of bytes to write to the current stream. |
callback |
An AsyncCallback delegate that references the method to call when the write operation is complete. |
state |
A user-defined object that distinguishes this particular call to BeginWrite from other calls. |
Return Value
| Type | Description |
|---|---|
IAsyncResult |
An IAsyncResult object that references the pending write operation. |
Exceptions
| Exception Type | Condition |
|---|---|
ArgumentNullException |
buffer is null. |
ArgumentOutOfRangeException |
offset is less than zero, or count is less than zero, or the sum of offset and count is greater than the length of buffer. |
ObjectDisposedException |
The stream has been closed. |
NotSupportedException |
SslStream does not support writing. |
Remarks
The BeginWrite method starts an asynchronous write operation. For a write operation to be completed, you must call the EndWrite method on the SslStream object.
If the stream is blocked, this method will block until the stream is unblocked.
To write data to an SslStream, you must first establish an SSL/TLS connection using BeginAuthenticateAsClient or BeginAuthenticateAsServer.
Note:
The
BeginWrite method is used to write data to the stream without blocking the calling thread. This is essential for applications that need to remain responsive during I/O operations, such as user interfaces or network servers.
Examples
The following code example demonstrates how to use the BeginWrite method to write data to an SslStream.
using System;
using System.Net.Security;
using System.Net.Sockets;
using System.Text;
using System.Security.Cryptography.X509Certificates;
public class SslClient
{
private TcpClient client;
private SslStream sslStream;
public void Connect(string host, int port)
{
client = new TcpClient(host, port);
sslStream = new SslStream(
client.GetStream(),
false,
new RemoteCertificateValidationCallback(ValidateServerCertificate),
null
);
sslStream.BeginAuthenticateAsClient(host, new AsyncCallback(AuthenticateCallback), null);
}
private bool ValidateServerCertificate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
{
if (sslPolicyErrors == SslPolicyErrors.None)
return true;
Console.WriteLine("Certificate error: {0}", sslPolicyErrors);
return false;
}
private void AuthenticateCallback(IAsyncResult ar)
{
try
{
sslStream.EndAuthenticateAsClient(ar);
string messageToSend = "Hello, Secure Server!";
byte[] messageBytes = Encoding.UTF8.GetBytes(messageToSend);
sslStream.BeginWrite(messageBytes, 0, messageBytes.Length, new AsyncCallback(WriteCallback), null);
}
catch (Exception ex)
{
Console.WriteLine($"Authentication failed: {ex.Message}");
}
}
private void WriteCallback(IAsyncResult ar)
{
try
{
sslStream.EndWrite(ar);
Console.WriteLine("Data written successfully.");
// Optionally, begin reading a response here
}
catch (Exception ex)
{
Console.WriteLine($"Write failed: {ex.Message}");
}
finally
{
// Close the stream and client when done
sslStream.Close();
client.Close();
}
}
}
Requirements
| Component | Version |
|---|---|
| .NET Framework | 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, .NET Core 2.0, .NET Standard 2.0 |
| .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 |
| Visual Studio | Visual Studio 2010 or later |