SslStream.BeginRead(Byte[], Int32, Int32, AsyncCallback, Object) Method

Definition

Begins an asynchronous read operation that reads data from the stream and stores it in the specified array.

C#
public override IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback? asyncCallback, object? asyncState);
C#
public override IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback asyncCallback, object asyncState);

Parameters

buffer
Byte[]

A Byte array that receives the bytes read from the stream.

offset
Int32

The zero-based location in buffer at which to begin storing the data read from this stream.

count
Int32

The maximum number of bytes to read from the stream.

asyncCallback
AsyncCallback

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

asyncState
Object

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

Returns

An IAsyncResult object that indicates the status of the asynchronous operation.

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 read operation failed.

-or-

Encryption is in use, but the data could not be decrypted.

There is already a read operation in progress.

This object has been closed.

Authentication has not occurred.

Examples

The following code example demonstrates starting an asynchronous read operation.

C#
// readData and buffer holds the data read from the server.
// They is used by the ReadCallback method.
static StringBuilder readData = new StringBuilder();
static byte [] buffer = new byte[2048];
C#
static void WriteCallback(IAsyncResult ar)
{
    SslStream stream = (SslStream) ar.AsyncState;
    try
    {
        Console.WriteLine("Writing data to the server.");
        stream.EndWrite(ar);
        // Asynchronously read a message from the server.
        stream.BeginRead(buffer, 0, buffer.Length,
            new AsyncCallback(ReadCallback),
            stream);
    }
    catch (Exception writeException)
    {
        e = writeException;
        complete = true;
        return;
    }
}

The following method is called when the read completes.

C#

static void ReadCallback(IAsyncResult ar)
{
    // Read the  message sent by the server.
    // The end of the message is signaled using the
    // "<EOF>" marker.
    SslStream stream = (SslStream) ar.AsyncState;
    int byteCount = -1;
    try
    {
        Console.WriteLine("Reading data from the server.");
        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(buffer,0, byteCount)];
        decoder.GetChars(buffer, 0, byteCount, chars,0);
        readData.Append (chars);
        // Check for EOF or an empty message.
        if (readData.ToString().IndexOf("<EOF>") == -1 && byteCount != 0)
        {
            // We are not finished reading.
            // Asynchronously read more message data from  the server.
            stream.BeginRead(buffer, 0, buffer.Length,
                new AsyncCallback(ReadCallback),
                stream);
        }
        else
        {
            Console.WriteLine("Message from the server: {0}", readData.ToString());
        }
    }
    catch (Exception readException)
    {
        e = readException;
        complete = true;
        return;
    }
    complete = true;
}

Remarks

If encryption and or signing are enabled, the read operation reads the data from the underlying stream, checks the integrity of the data, and/or decrypts it. The asynchronous read operation must be completed by calling the EndRead method. Typically, the method is invoked by the asyncCallback delegate.

This method does not block while the operation completes. To block until the operation completes, use the Read method.

For detailed information about using the asynchronous programming model, see Calling Synchronous Methods Asynchronously

The SslStream class does not support multiple simultaneous read operations.

You cannot call this method until you have successfully authenticated. To authenticate call one of the AuthenticateAsClient, or BeginAuthenticateAsClient, AuthenticateAsServer, BeginAuthenticateAsServer methods.

Applies to

Product Versions
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 2.0, 3.0, 3.5, 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, 4.8.1
.NET Standard 2.0, 2.1