SslStream.BeginRead(Byte[], Int32, Int32, AsyncCallback, Object) SslStream.BeginRead(Byte[], Int32, Int32, AsyncCallback, Object) SslStream.BeginRead(Byte[], Int32, Int32, AsyncCallback, Object) Method

定義

ストリームからデータを読み取り、指定した配列に格納する非同期読み取り操作を開始します。Begins an asynchronous read operation that reads data from the stream and stores it in the specified array.

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);
override this.BeginRead : byte[] * int * int * AsyncCallback * obj -> IAsyncResult

パラメーター

buffer
Byte[]

ストリームから読み取ったバイトを受け取る Byte 配列。A Byte array that receives the bytes read from the stream.

offset
Int32 Int32 Int32

内の 0 から始まる場所bufferこのストリームから読み取ったデータの格納を開始する位置。The zero-based location in buffer at which to begin storing the data read from this stream.

count
Int32 Int32 Int32

ストリームから読み取る最大バイト数。The maximum number of bytes to read from the stream.

asyncCallback
AsyncCallback AsyncCallback AsyncCallback

読み取り操作の完了時に呼び出すメソッドを参照する AsyncCallback デリゲート。An AsyncCallback delegate that references the method to invoke when the read operation is complete.

asyncState
Object Object Object

読み取り操作に関する情報を格納するユーザー定義のオブジェクト。A user-defined object that contains information about the read operation. このオブジェクトは、操作の完了時に asyncCallback デリゲートに渡されます。This object is passed to the asyncCallback delegate when the operation completes.

戻り値

非同期操作のステータスを示す IAsyncResult オブジェクト。An IAsyncResult object that indicates the status of the asynchronous operation.

例外

buffernull です。buffer is null.

offset < 0.offset < 0.

-or-offset > buffer の長さ。-or-offset > the length of buffer.

- または --or- offset+ count > buffer の長さ。offset + count > the length of buffer.

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

- または --or- 暗号化が使用されていますが、データを復号化できませんでした。Encryption is in use, but the data could not be decrypted.

既に実行中の読み取り操作が存在します。There is already a read operation in progress.

このオブジェクトは閉じられました。This object has been closed.

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

次のコード例では、非同期読み取り操作の開始を示します。The following code example demonstrates starting an asynchronous read operation.

// 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

読み取りが完了すると、次のメソッドが呼び出されます。The following method is called when the read completes.

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 encryption and or signing are enabled, the read operation reads the data from the underlying stream, checks the integrity of the data, and/or decrypts it. 呼び出すことによって、非同期読み取り操作を完了する必要があります、EndReadメソッド。The asynchronous read operation must be completed by calling the EndRead method. 通常、メソッドは、によって呼び出される、asyncCallbackを委任します。Typically, the method is invoked by the asyncCallback delegate.

このメソッドは、操作が完了するにはブロックしません。This method does not block while the operation completes. 操作が完了するまでにブロックするを使用して、Readメソッド。To block until the operation completes, use the Read method.

詳細については、非同期プログラミング モデルを使用して、次を参照してくださいCalling Synchronous Methods Asynchronously。For detailed information about using the asynchronous programming model, see Calling Synchronous Methods Asynchronously

SslStreamクラスは、複数の同時読み取り操作をサポートしていません。The SslStream class does not support multiple simultaneous read operations.

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

適用対象