Share via


NegotiateStream.EndRead(IAsyncResult) Méthode

Définition

Termine une opération de lecture asynchrone qui a été démarrée avec un appel à BeginRead(Byte[], Int32, Int32, AsyncCallback, Object).

public:
 override int EndRead(IAsyncResult ^ asyncResult);
public override int EndRead (IAsyncResult asyncResult);
override this.EndRead : IAsyncResult -> int
Public Overrides Function EndRead (asyncResult As IAsyncResult) As Integer

Paramètres

asyncResult
IAsyncResult

Instance de IAsyncResult retournée par un appel à BeginRead(Byte[], Int32, Int32, AsyncCallback, Object).

Retours

Valeur Int32 qui spécifie le nombre d'octets lus dans le flux sous-jacent.

Exceptions

asyncResult a la valeur null.

asyncResult n'a pas été créé par un appel à BeginRead(Byte[], Int32, Int32, AsyncCallback, Object).

Aucune opération de lecture n'est en attente d'achèvement.

- ou -

L'authentification n'a pas été effectuée.

L'opération de lecture a échoué.

Exemples

L’exemple de code suivant illustre la fin d’une opération de lecture asynchrone. Pour obtenir un exemple illustrant le démarrage de l’opération, consultez BeginRead.

static void EndReadCallback( IAsyncResult^ ar )
{
   
   // Get the saved data.
   ClientState^ cState = dynamic_cast<ClientState^>(ar->AsyncState);
   TcpClient^ clientRequest = cState->Client;
   NegotiateStream^ authStream = dynamic_cast<NegotiateStream^>(cState->AuthStream);
   
   // Get the buffer that stores the message sent by the client.
   int bytes = -1;
   
   // Read the client message.
   try
   {
      bytes = authStream->EndRead( ar );
      cState->Message->Append( Encoding::UTF8->GetChars( cState->Buffer, 0, bytes ) );
      if ( bytes != 0 )
      {
         authStream->BeginRead( cState->Buffer, 0, cState->Buffer->Length, gcnew AsyncCallback( EndReadCallback ), cState );
         return;
      }
   }
   catch ( Exception^ e ) 
   {
      
      // A real application should do something
      // useful here, such as logging the failure.
      Console::WriteLine( L"Client message exception:" );
      Console::WriteLine( e );
      cState->Waiter->Set();
      return;
   }

   IIdentity^ id = authStream->RemoteIdentity;
   Console::WriteLine( L"{0} says {1}", id->Name, cState->Message );
   cState->Waiter->Set();
}

private static void EndReadCallback(ClientState cState, int bytes)
{
    NegotiateStream authStream = (NegotiateStream)cState.AuthenticatedStream;
    // Read the client message.
    try
    {
        cState.Message.Append(Encoding.UTF8.GetChars(cState.Buffer, 0, bytes));
        if (bytes != 0)
        {
            Task<int> readTask = authStream.ReadAsync(cState.Buffer, 0, cState.Buffer.Length);
            readTask
                .ContinueWith(task => { EndReadCallback(cState, task.Result); })
                .Wait();

            return;
        }
    }
    catch (Exception e)
    {
        // A real application should do something
        // useful here, such as logging the failure.
        Console.WriteLine("Client message exception:");
        Console.WriteLine(e);
        return;
    }
    IIdentity id = authStream.RemoteIdentity;
    Console.WriteLine("{0} says {1}", id.Name, cState.Message.ToString());
}

Remarques

Si l’opération n’est pas terminée, cette méthode se bloque jusqu’à ce qu’elle le fasse.

Pour effectuer cette opération de manière synchrone, utilisez la Read méthode .

Vous ne pouvez pas appeler cette méthode tant que vous n’avez pas réussi à vous authentifier. Pour vous authentifier, appelez l’une AuthenticateAsClientdes méthodes , AuthenticateAsClientAsyncBeginAuthenticateAsClient, AuthenticateAsServer, , AuthenticateAsServerAsyncou BeginAuthenticateAsServer .

S’applique à