SslStream.BeginRead(Byte[], Int32, Int32, AsyncCallback, Object) メソッド

定義

ストリームからデータを読み取り、指定した配列に格納する非同期読み取り操作を開始します。

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
Public Overrides Function BeginRead (buffer As Byte(), offset As Integer, count As Integer, asyncCallback As AsyncCallback, asyncState As Object) As IAsyncResult

パラメーター

buffer
Byte[]

ストリームから読み取られたバイト数を受け取る Byte 配列。

offset
Int32

このストリームから読み取られたデータの格納を開始する buffer の 0 から始まる位置。

count
Int32

ストリームから読み取る最大バイト数。

asyncCallback
AsyncCallback

読み取り操作が完了したときに呼び出すメソッドを参照する AsyncCallback デリゲート。

asyncState
Object

読み取り操作に関する情報を含むユーザー定義オブジェクト。 このオブジェクトは、操作の完了時に asyncCallback デリゲートに渡されます。

返品

非同期操作の状態を示す IAsyncResult オブジェクト。

例外

buffernullです。

offset が 0 未満です。

-または-

offsetbufferの長さを超えています。

-または-

offset + count が bufferの長さを超えています。

読み取り操作に失敗しました。

-または-

暗号化は使用中ですが、データの暗号化を解除できませんでした。

既に読み取り操作が進行中です。

このオブジェクトは閉じられています。

認証が行われません。

次のコード例は、非同期読み取り操作を開始する方法を示しています。

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

読み取りが完了すると、次のメソッドが呼び出されます。


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

注釈

暗号化または署名が有効になっている場合、読み取り操作は基になるストリームからデータを読み取り、データの整合性をチェックし、データの暗号化を解除します。 非同期読み取り操作は、 EndRead メソッドを呼び出すことによって完了する必要があります。 通常、メソッドは asyncCallback デリゲートによって呼び出されます。

このメソッドは、操作の完了中はブロックしません。 操作が完了するまでブロックするには、 Read メソッドを使用します。

非同期プログラミング モデルの使用の詳細については、「非同期メソッドの呼び出し」を参照してください。

SslStream クラスは、複数の同時読み取り操作をサポートしていません。

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

適用対象