SslStream.Write Method
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Writes data to this stream.
Overloads
| Write(Byte[]) |
Writes the specified data to this stream. |
| Write(ReadOnlySpan<Byte>) |
When overridden in a derived class, writes a sequence of bytes to the current stream and advances the current position within this stream by the number of bytes written. |
| 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
- Source:
- SslStream.cs
Writes the specified data to this stream.
public:
void Write(cli::array <System::Byte> ^ buffer);
public void Write(byte[] buffer);
override this.Write : byte[] -> unit
Public Sub Write (buffer As Byte())
Parameters
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.
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();
}
}
Private Shared Sub ProcessClient(client As TcpClient)
' A client has connected. Create the
' SslStream using the client's network stream.
Dim sslStream = New SslStream(client.GetStream(), False)
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...")
Dim messageData As String = ReadMessage(sslStream)
Console.WriteLine("Received: {0}", messageData)
' Write a message to the client.
Dim message As Byte() = Encoding.UTF8.GetBytes("Hello from the server.<EOF>")
Console.WriteLine("Sending hello message.")
sslStream.Write(message)
Catch e As AuthenticationException
Console.WriteLine("Exception: {0}", e.Message)
If e.InnerException IsNot Nothing Then
Console.WriteLine("Inner exception: {0}", e.InnerException.Message)
End If
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()
End Try
End Sub
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
Write(ReadOnlySpan<Byte>)
- Source:
- SslStream.cs
- Source:
- SslStream.cs
When overridden in a derived class, writes a sequence of bytes to the current stream and advances the current position within this stream by the number of bytes written.
public:
override void Write(ReadOnlySpan<System::Byte> buffer);
public override void Write(ReadOnlySpan<byte> buffer);
override this.Write : ReadOnlySpan<byte> -> unit
Public Overrides Sub Write (buffer As ReadOnlySpan(Of Byte))
Parameters
- buffer
- ReadOnlySpan<Byte>
A region of memory. This method copies the contents of this region to the current stream.
Applies to
Write(Byte[], Int32, Int32)
- Source:
- SslStream.cs
- 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.
public:
override void Write(cli::array <System::Byte> ^ buffer, int offset, int count);
public override void Write(byte[] buffer, int offset, int count);
override this.Write : byte[] * int * int -> unit
Public Overrides Sub Write (buffer As Byte(), offset As Integer, count As Integer)
Parameters
- offset
- Int32
A Int32 that contains the zero-based location in buffer at which to begin reading bytes to be written to the stream.
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.