SslStream.EndRead(IAsyncResult) Método

Definición

Finaliza una operación de lectura asincrónica iniciada con una llamada previa a BeginRead(Byte[], Int32, Int32, AsyncCallback, Object).

C#
public override int EndRead(IAsyncResult asyncResult);

Parámetros

asyncResult
IAsyncResult

Una instancia IAsyncResult devuelta por una llamada a BeginRead(Byte[], Int32, Int32, AsyncCallback, Object).

Devoluciones

Valor Int32 que especifica el número de bytes leídos de la secuencia subyacente.

Excepciones

asyncResult es null.

El parámetro asyncResult no se creó mediante una llamada a BeginRead(Byte[], Int32, Int32, AsyncCallback, Object).

No queda ninguna operación de lectura sin finalizar.

O bien

No se ha producido la autenticación.

Se ha producido un error en la operación de lectura.

Ejemplos

En el ejemplo de código siguiente se muestra cómo finalizar una operación de lectura asincrónica.

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;
}

Comentarios

Si la operación no se ha completado, este método se bloquea hasta que lo haga.

Para realizar esta operación de forma sincrónica, use el Read método .

No puede llamar a este método hasta que se haya autenticado correctamente. Para autenticar una de las llamadas a uno de los AuthenticateAsClientmétodos , o BeginAuthenticateAsClient, AuthenticateAsServer. BeginAuthenticateAsServer

Se aplica a

Producto Versiones
.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