SslStream.BeginRead(Byte[], Int32, Int32, AsyncCallback, Object) Yöntem

Tanım

Akıştaki verileri okuyan ve belirtilen dizide depolayan zaman uyumsuz bir okuma işlemi başlatır.

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

Parametreler

buffer
Byte[]

Byte Akıştan okunan baytları alan bir dizi.

offset
Int32

Bu akıştan okunan verilerin depolandığı sıfır tabanlı konum buffer .

count
Int32

Akıştan okunacak bayt sayısı üst sınırı.

asyncCallback
AsyncCallback

AsyncCallback Okuma işlemi tamamlandığında çağırma yöntemine başvuran bir temsilci.

asyncState
Object

Okuma işlemi hakkında bilgi içeren kullanıcı tanımlı bir nesne. İşlem tamamlandığında bu nesne temsilciye asyncCallback geçirilir.

Döndürülenler

IAsyncResult

IAsyncResult Zaman uyumsuz işlemin durumunu gösteren nesne.

Özel durumlar

buffer, null değeridir.

offset, sıfırdan küçüktür.

-veya-

offset , uzunluğundan bufferbüyüktür.

-veya- offset + sayı, uzunluğundan bufferbüyüktür.

Okuma işlemi başarısız oldu.

-veya-

Şifreleme kullanılıyor, ancak verilerin şifresi çözülemedi.

Devam eden bir okuma işlemi zaten var.

Bu nesne kapatıldı.

Kimlik doğrulaması gerçekleşmedi.

Örnekler

Aşağıdaki kod örneği, zaman uyumsuz okuma işlemini başlatmayı gösterir.

// readData and buffer holds the data read from the server.
// They is used by the ReadCallback method.
static StringBuilder^ readData = gcnew StringBuilder;
static array<Byte>^buffer = gcnew array<Byte>(2048);
// readData and buffer holds the data read from the server.
// They is used by the ReadCallback method.
static StringBuilder readData = new StringBuilder();
static byte [] buffer = new byte[2048];
' readData and buffer holds the data read from the server.
' They is used by the ReadCallback method.
Shared readData As New StringBuilder()
Shared buffer As Byte() = New Byte(2048) {}
static void WriteCallback( IAsyncResult^ ar )
{
   SslStream^ stream = dynamic_cast<SslStream^>(ar->AsyncState);
   try
   {
      Console::WriteLine( L"Writing data to the server." );
      stream->EndWrite( ar );
      
      // Asynchronously read a message from the server.
      stream->BeginRead( buffer, 0, buffer->Length, gcnew AsyncCallback( ReadCallback ), stream );
   }
   catch ( Exception^ writeException ) 
   {
      e = writeException;
      complete = true;
      return;
   }

}
static void WriteCallback(IAsyncResult ar)
{
    SslStream stream = (SslStream) ar.AsyncState;
    try
    {
        Console.WriteLine("Writing data to the server.");
        stream.EndWrite(ar);
        // Asynchronously read a message from the server.
        stream.BeginRead(buffer, 0, buffer.Length,
            new AsyncCallback(ReadCallback),
            stream);
    }
    catch (Exception writeException)
    {
        e = writeException;
        complete = true;
        return;
    }
}
Shared Sub WriteCallback(ar As IAsyncResult)
    Dim stream = CType(ar.AsyncState, SslStream)
    Try
        Console.WriteLine("Writing data to the server.")
        stream.EndWrite(ar)
        ' Asynchronously read a message from the server.
        stream.BeginRead(buffer, 0, buffer.Length, New AsyncCallback(AddressOf ReadCallback), stream)
    Catch writeException As Exception
        e = writeException
        complete = True
        Return
    End Try
End Sub

Okuma tamamlandığında aşağıdaki yöntem çağrılır.

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

Açıklamalar

Şifreleme ve veya imzalama etkinse, okuma işlemi temel alınan akıştaki verileri okur, verilerin bütünlüğünü denetler ve/veya şifresini çözer. Zaman uyumsuz okuma işlemi yöntemi çağrılarak EndRead tamamlanmalıdır. Genellikle yöntemi temsilci tarafından çağrılır asyncCallback .

İşlem tamamlarken bu yöntem engellemez. İşlem tamamlanana kadar engellemek için yöntemini kullanın Read .

Zaman uyumsuz programlama modelini kullanma hakkında ayrıntılı bilgi için bkz. Zaman Uyumlu Yöntemleri Zaman Uyumsuz Olarak Çağırma

SslStream sınıfı birden çok eşzamanlı okuma işlemini desteklemez.

Kimlik doğrulaması başarılı olana kadar bu yöntemi çağıramazsınız. Kimlik doğrulaması yapmak için , veya BeginAuthenticateAsClient, AuthenticateAsServerBeginAuthenticateAsServer yöntemlerinden birini AuthenticateAsClientçağırın.

Şunlara uygulanır