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

BeginRead(Byte[], Int32, Int32, AsyncCallback, Object) の呼び出しによって返される IAsyncResult インスタンス。

戻り値

基になるストリームから読み取るバイト数を指定する Int32 値。

例外

asyncResultnullです。

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

正常に認証されるまで、このメソッドを呼び出すことはできません。 認証するには、、BeginAuthenticateAsClientAuthenticateAsServerAuthenticateAsServerAsync、または BeginAuthenticateAsServer のいずれかのAuthenticateAsClientAuthenticateAsClientAsyncメソッドを呼び出します。

適用対象