NegotiateStream.EndRead(IAsyncResult) 메서드

정의

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

매개 변수

asyncResult
IAsyncResult

IAsyncResult를 호출했을 때 반환되는 BeginRead(Byte[], Int32, Int32, AsyncCallback, Object) 인스턴스입니다.

반환

Int32

기본 스트림에서 읽은 바이트 수를 지정하는 Int32 값입니다.

예외

asyncResult이(가) null인 경우

BeginRead(Byte[], Int32, Int32, AsyncCallback, Object)를 호출했지만 asyncResult가 만들어지지 않은 경우

완료할 보류 상태의 읽기 작업이 없는 경우

또는

인증이 수행되지 않은 경우.

읽기 작업이 실패한 경우.

예제

다음 코드 예제에서는 비동기 읽기 작업을 종료하는 방법을 보여 줍니다. 작업을 시작하는 방법을 보여 주는 예제는 다음을 참조하세요 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());
}

설명

작업이 완료되지 않은 경우 이 메서드는 작업이 완료될 때까지 차단합니다.

이 작업을 동기적으로 수행하려면 이 메서드를 Read 사용합니다.

성공적으로 인증될 때까지 이 메서드를 호출할 수 없습니다. 인증하려면 , , AuthenticateAsClientAsync, AuthenticateAsServerAsyncBeginAuthenticateAsClientAuthenticateAsServer또는 BeginAuthenticateAsServer 메서드 중 AuthenticateAsClient하나를 호출합니다.

적용 대상