SslStream.EndRead(IAsyncResult) メソッド

定義

BeginRead(Byte[], Int32, Int32, AsyncCallback, Object) の以前の呼び出しで開始した非同期読み取り操作を終了します。Ends an asynchronous read operation started with a previous 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

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

戻り値

Int32

基になるストリームから読み取るバイト数を指定する 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 が作成されませんでした。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.

static void ReadCallback( IAsyncResult^ ar )
{
   
   // Read the  message sent by the server.
   // The end of the message is signaled using the
   // "<EOF>" marker.
   SslStream^ stream = dynamic_cast<SslStream^>(ar->AsyncState);
   int byteCount = -1;
   try
   {
      Console::WriteLine( L"Reading data from the server." );
      byteCount = stream->EndRead( ar );
      
      // Use Decoder class to convert from bytes to UTF8
      // in case a character spans two buffers.
      Decoder^ decoder = Encoding::UTF8->GetDecoder();
      array<Char>^chars = gcnew array<Char>(decoder->GetCharCount( buffer, 0, byteCount ));
      decoder->GetChars( buffer, 0, byteCount, chars, 0 );
      readData->Append( chars );
      
      // Check for EOF or an empty message.
      if ( readData->ToString()->IndexOf( L"<EOF>" ) == -1 && byteCount != 0 )
      {
         
         // We are not finished reading.
         // Asynchronously read more message data from  the server.
         stream->BeginRead( buffer, 0, buffer->Length, gcnew AsyncCallback( ReadCallback ), stream );
      }
      else
      {
         Console::WriteLine( L"Message from the server: {0}", readData );
      }
   }
   catch ( Exception^ readException ) 
   {
      e = readException;
      complete = true;
      return;
   }

   complete = true;
}



static void ReadCallback(IAsyncResult ar)
{
    // Read the  message sent by the server.
    // The end of the message is signaled using the
    // "<EOF>" marker.
    SslStream stream = (SslStream) ar.AsyncState;
    int byteCount = -1;
    try
    {
        Console.WriteLine("Reading data from the server.");
        byteCount = stream.EndRead(ar);
        // Use Decoder class to convert from bytes to UTF8
        // in case a character spans two buffers.
        Decoder decoder = Encoding.UTF8.GetDecoder();
        char[] chars = new char[decoder.GetCharCount(buffer,0, byteCount)];
        decoder.GetChars(buffer, 0, byteCount, chars,0);
        readData.Append (chars);
        // Check for EOF or an empty message.
        if (readData.ToString().IndexOf("<EOF>") == -1 && byteCount != 0)
        {
            // We are not finished reading.
            // Asynchronously read more message data from  the server.
            stream.BeginRead(buffer, 0, buffer.Length,
                new AsyncCallback(ReadCallback),
                stream);
        }
        else
        {
            Console.WriteLine("Message from the server: {0}", readData.ToString());
        }
    }
    catch (Exception readException)
    {
        e = readException;
        complete = true;
        return;
    }
    complete = true;
}

Shared Sub ReadCallback(ar As IAsyncResult)
    ' Read the  message sent by the server.
    ' The end of the message is signaled using the
    ' "<EOF>" marker.
    Dim stream = CType(ar.AsyncState, SslStream)
    Dim byteCount As Integer
    Try
        Console.WriteLine("Reading data from the server.")
        byteCount = stream.EndRead(ar)
        ' Use Decoder class to convert from bytes to UTF8
        ' in case a character spans two buffers.
        Dim decoder As Decoder = Encoding.UTF8.GetDecoder()
        Dim chars = New Char(decoder.GetCharCount(buffer, 0, byteCount)) {}
        decoder.GetChars(buffer, 0, byteCount, chars, 0)
        readData.Append(chars)
        ' Check for EOF or an empty message.
        If readData.ToString().IndexOf("<EOF>") = -1 AndAlso byteCount <> 0 Then
            ' We are not finished reading.
            ' Asynchronously read more message data from  the server.
            stream.BeginRead(buffer, 0, buffer.Length, New AsyncCallback(AddressOf ReadCallback), stream)
        Else
            Console.WriteLine("Message from the server: {0}", readData.ToString())
        End If
    Catch readException As Exception
        e = readException
        complete = True
        Return
    End Try
    complete = True
End Sub

注釈

操作が完了していない場合、このメソッドは、処理が完了するまでブロックします。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. 認証するには、、、、、のいずれかのメソッドを呼び出し AuthenticateAsClient BeginAuthenticateAsClient AuthenticateAsServer BeginAuthenticateAsServer ます。To authenticate call one of the AuthenticateAsClient, or BeginAuthenticateAsClient, AuthenticateAsServer, BeginAuthenticateAsServer methods.

適用対象