NegotiateStream.BeginRead(Byte[], Int32, Int32, AsyncCallback, Object) 메서드

정의

스트림에서 데이터를 읽어 지정된 배열에 저장하는 비동기 읽기 작업을 시작합니다.

public:
 override IAsyncResult ^ BeginRead(cli::array <System::Byte> ^ buffer, int offset, int count, AsyncCallback ^ asyncCallback, System::Object ^ asyncState);
public override IAsyncResult BeginRead (byte[] buffer, int offset, int count, AsyncCallback? asyncCallback, object? asyncState);
public override IAsyncResult BeginRead (byte[] buffer, int offset, int count, AsyncCallback asyncCallback, object asyncState);
override this.BeginRead : byte[] * int * int * AsyncCallback * obj -> IAsyncResult
Public Overrides Function BeginRead (buffer As Byte(), offset As Integer, count As Integer, asyncCallback As AsyncCallback, asyncState As Object) As IAsyncResult

매개 변수

buffer
Byte[]

스트림에서 읽은 바이트를 받는 Byte 배열입니다.

offset
Int32

이 스트림에서 읽은 데이터를 저장하기 시작하는, buffer 내의 0부터 시작하는 위치입니다.

count
Int32

스트림에서 읽을 최대 바이트 수입니다.

asyncCallback
AsyncCallback

읽기 작업이 완료되었을 때 호출할 메서드를 참조하는 AsyncCallback 대리자입니다.

asyncState
Object

읽기 작업에 대한 정보가 포함된 사용자 정의 개체입니다. 작업이 완료되면 asyncCallback 대리자에게 전달되는 개체입니다.

반환

IAsyncResult

비동기 작업의 상태를 표시하는 IAsyncResult 개체입니다.

예외

buffer이(가) null인 경우

offset 가 0보다 작습니다.

또는 offsetbuffer의 길이보다 큽니다.

또는 offset + countbuffer의 길이보다 큽니다.

읽기 작업이 실패한 경우.

또는

암호화를 사용 중이지만 데이터를 해독할 수 없는 경우.

읽기 작업을 진행 중인 경우

이 개체가 닫힌 경우.

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

예제

다음 코드 예제에서는 비동기 읽기 작업을 시작하는 방법을 보여 줍니다. 이 코드 예제는에 대해 제공 된 큰 예제의 일부는 NegotiateStream 클래스입니다.

static void AuthenticateClient( TcpClient^ clientRequest )
{
   NetworkStream^ stream = clientRequest->GetStream();
   
   // Create the NegotiateStream.
   NegotiateStream^ authStream = gcnew NegotiateStream( stream,false );
   
   // Save the current client and NegotiateStream instance 
   // in a ClientState object.
   ClientState^ cState = gcnew ClientState( authStream,clientRequest );
   
   // Listen for the client authentication request.
   authStream->BeginAuthenticateAsServer( gcnew AsyncCallback( EndAuthenticateCallback ), cState );
   
   // Wait until the authentication completes.
   cState->Waiter->WaitOne();
   cState->Waiter->Reset();
   authStream->BeginRead( cState->Buffer, 0, cState->Buffer->Length, gcnew AsyncCallback( EndReadCallback ), cState );
   cState->Waiter->WaitOne();
   
   // Finished with the current client.
   authStream->Close();
   clientRequest->Close();
}


public static void AuthenticateClient(TcpClient clientRequest)
{
    NetworkStream stream = clientRequest.GetStream();
    // Create the NegotiateStream.
    NegotiateStream authStream = new NegotiateStream(stream, false);
    // Save the current client and NegotiateStream instance
    // in a ClientState object.
    ClientState cState = new ClientState(authStream, clientRequest);
    // Listen for the client authentication request.
    Task authTask = authStream
        .AuthenticateAsServerAsync()
        .ContinueWith(task => { EndAuthenticateCallback(cState); });

    // Any exceptions that occurred during authentication are
    // thrown by the EndAuthenticateAsServer method.
    try
    {
        // This call blocks until the authentication is complete.
        authTask.Wait();
    }
    catch (AuthenticationException e)
    {
        Console.WriteLine(e);
        Console.WriteLine("Authentication failed - closing connection.");
        return;
    }
    catch (Exception e)
    {
        Console.WriteLine(e);
        Console.WriteLine("Closing connection.");
        return;
    }

    Task<int> readTask = authStream
        .ReadAsync(cState.Buffer, 0, cState.Buffer.Length);

    readTask
        .ContinueWith((task) => { EndReadCallback(cState, task.Result); })
        .Wait();
    // Finished with the current client.
    authStream.Close();
    clientRequest.Close();
}

설명

암호화, 서명 또는 암호화 및 서명을 사용하는 경우 읽기 작업은 기본 스트림에서 데이터를 읽고 데이터의 무결성을 확인하고 암호를 해독합니다. 데이터 암호화 또는 서명과 같은 보안 서비스가 사용되지 않는 경우 이 메서드는 기본 스트림에서 비동기 읽기 작업을 시작합니다.

이 메서드는 비동기이며 작업이 완료되는 동안 차단되지 않습니다. 작업이 완료될 때까지 차단하려면 메서드를 Read 사용합니다.

메서드를 호출 EndRead 하여 비동기 읽기 작업을 완료해야 합니다. 일반적으로 메서드는 대리자에서 호출됩니다 asyncCallback . 비동기 프로그래밍 모델 사용에 대한 자세한 내용은 동기 메서드 호출을 비동기적으로 참조하세요.

클래스는 NegotiateStream 여러 동시 읽기 작업을 지원하지 않습니다. 다른 읽기 작업이 동일한 스트림에서 이미 실행되는 동안 읽기 작업을 시작하려고 하면 예외가 NotSupportedException throw됩니다.

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

적용 대상