Bagikan melalui


NegotiateStream.EndRead(IAsyncResult) Metode

Definisi

Mengakhiri operasi baca asinkron yang dimulai dengan panggilan ke 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

Parameter

asyncResult
IAsyncResult

Instans IAsyncResult yang dikembalikan oleh panggilan ke BeginRead(Byte[], Int32, Int32, AsyncCallback, Object).

Mengembalikan

Int32 Nilai yang menentukan jumlah byte yang dibaca dari aliran yang mendasar.

Pengecualian

asyncResultadalah null.

Tidak ada operasi baca yang tertunda untuk diselesaikan.

-atau-

Autentikasi belum terjadi.

Operasi baca gagal.

Contoh

Contoh kode berikut menunjukkan mengakhiri operasi baca asinkron. Untuk contoh yang menunjukkan memulai operasi, lihat 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());
}

Keterangan

Jika operasi belum selesai, metode ini memblokir sampai operasi selesai.

Untuk melakukan operasi ini secara sinkron, gunakan Read metode .

Anda tidak dapat memanggil metode ini sampai Anda berhasil mengautentikasi. Untuk mengautentikasi, panggil salah AuthenticateAsClientsatu metode , , AuthenticateAsClientAsyncBeginAuthenticateAsClient, AuthenticateAsServer, AuthenticateAsServerAsyncatau BeginAuthenticateAsServer .

Berlaku untuk