This documentation is archived and is not being maintained.

SslStream.BeginWrite Method

Begins an asynchronous write operation that writes Bytes from the specified buffer to the stream.

Namespace: System.Net.Security
Assembly: System (in system.dll)

public override IAsyncResult BeginWrite (
	byte[] buffer,
	int offset,
	int count,
	AsyncCallback asyncCallback,
	Object asyncState
public IAsyncResult BeginWrite (
	byte[] buffer, 
	int offset, 
	int count, 
	AsyncCallback asyncCallback, 
	Object asyncState
public override function BeginWrite (
	buffer : byte[], 
	offset : int, 
	count : int, 
	asyncCallback : AsyncCallback, 
	asyncState : Object
) : IAsyncResult
Not applicable.



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


The zero-based location in buffer at which to begin reading bytes to be written to the stream.


An Int32 value that specifies the number of bytes to read from buffer.


An AsyncCallback delegate that references the method to invoke when the write operation is complete.


A user-defined object that contains information about the write operation. This object is passed to the asyncCallback delegate when the operation completes.

Return Value

An IAsyncResult object indicating the status of the asynchronous operation.

Exception typeCondition


buffer is a null reference (Nothing in Visual Basic).


offset < 0.


offset > the length of buffer.


offset + count > 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.

The following code example demonstrates calling this method.

void ReadCallback(IAsyncResult ar)
    ClientState state = (ClientState) ar.AsyncState;
    SslStream stream =;
    // Read the  message sent by the client.
    // The end of the message is signaled using the
    // "<EOF>" marker.
    int byteCount = -1;
        Console.WriteLine("Reading data from the client.");
        byteCount = stream.EndRead(ar);
        // Use Decoder class to convert from bytes to UTF8
        // in case a character spans two buffers.
        Decoder decoder = Encoding.UTF8.GetDecoder();
        char[] chars = new char[decoder.GetCharCount(state.buffer,0, byteCount)];
        decoder.GetChars(state.buffer, 0, byteCount, chars,0);
        state.readData.Append (chars);
        // Check for EOF or an empty message.
        if (state.readData.ToString().IndexOf("<EOF>") == -1 && byteCount != 0)
            // We are not finished reading.
            // Asynchronously read more message data from  the client.
            stream.BeginRead(state.buffer, 0, state.buffer.Length, 
                new AsyncCallback(ReadCallback),
            Console.WriteLine("Message from the client: {0}", state.readData.ToString());
        // Encode a test message into a byte array.
        // Signal the end of the message using "<EOF>".
        byte[] message = Encoding.UTF8.GetBytes("Hello from the server.<EOF>");
        // Asynchronously send the message to the client.
        stream.BeginWrite(message, 0, message.Length, 
            new AsyncCallback(WriteCallback),
    catch (Exception readException)
        Console.WriteLine("Read error: {0}", readException.Message);

Windows 98, Windows Server 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0