NegotiateStream.EndRead(IAsyncResult) メソッド

定義

BeginRead(Byte[], Int32, Int32, AsyncCallback, Object) の呼び出しで開始された、非同期の読み取り操作を終了します。Ends an asynchronous read operation that was started with a call to 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) インスタンス。An IAsyncResult instance returned by a call to BeginRead(Byte[], Int32, Int32, AsyncCallback, Object)

戻り値

基になるストリームから読み取るバイト数を指定する Int32 値。A Int32 value that specifies the number of bytes read from the underlying stream.

例外

asyncResultnull です。asyncResult is null.

BeginRead(Byte[], Int32, Int32, AsyncCallback, Object) の呼び出しで asyncResult が作成されませんでした。The asyncResult was not created by a call to BeginRead(Byte[], Int32, Int32, AsyncCallback, Object).

完了する保留中の読み取り操作がありません。There is no pending read operation to complete.

または-or-

認証が行われていません。Authentication has not occurred.

読み取り操作に失敗しました。The read operation failed.

次のコード例は、非同期の読み取り操作を終了する方法を示しています。The following code example demonstrates ending an asynchronous read operation. 操作の開始方法を示す例については、「BeginRead」を参照してください。For an example that demonstrates starting the operation, see 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();
}

public static void EndReadCallback(IAsyncResult ar)
{
    // Get the saved data.
    ClientState cState = (ClientState) ar.AsyncState;
    TcpClient clientRequest = cState.Client;
    NegotiateStream authStream = (NegotiateStream) cState.AuthenticatedStream; 
    // 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, 
                       new AsyncCallback(EndReadCallback), 
                       cState);
                       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);
        cState.Waiter.Set();
        return;
    }
    IIdentity id = authStream.RemoteIdentity;
    Console.WriteLine("{0} says {1}", id.Name, cState.Message.ToString());
    cState.Waiter.Set();
}

注釈

操作が完了していない場合、このメソッドは、処理が完了するまでブロックします。If the operation has not completed, this method blocks until it does.

この操作を同期的に実行するには、Read メソッドを使用します。To perform this operation synchronously, use the Read method.

が正常に認証されるまで、このメソッドを呼び出すことはできません。You cannot call this method until you have successfully authenticated. 認証するには、AuthenticateAsClientBeginAuthenticateAsClientAuthenticateAsServer、または BeginAuthenticateAsServer メソッドのいずれかを呼び出します。To authenticate, call one of the AuthenticateAsClient, BeginAuthenticateAsClient, AuthenticateAsServer, or BeginAuthenticateAsServer methods.

適用対象