NegotiateStream.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);
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 の長さを超えています。

または

offsetcount を加算した値が、buffer の長さを超えています。

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

または

暗号化が使用されていますが、データを復号化できませんでした。

既に実行中の読み取り操作が存在します。

このオブジェクトは閉じられました。

認証が行われていません。

次のコード例は、非同期読み取り操作を開始する方法を示しています。 このコード例は、NegotiateStream クラスのために提供されている大規模な例の一部です。

static void AuthenticateClient( TcpClient^ clientRequest )
{
   NetworkStream^ stream = clientRequest->GetStream();
   
   // Create the NegotiateStream.
   NegotiateStream^ authStream = gcnew NegotiateStream( stream,false );
   
   // Save the current client and NegotiateStream instance 
   // in a ClientState object.
   ClientState^ cState = gcnew ClientState( authStream,clientRequest );
   
   // Listen for the client authentication request.
   authStream->BeginAuthenticateAsServer( gcnew AsyncCallback( EndAuthenticateCallback ), cState );
   
   // Wait until the authentication completes.
   cState->Waiter->WaitOne();
   cState->Waiter->Reset();
   authStream->BeginRead( cState->Buffer, 0, cState->Buffer->Length, gcnew AsyncCallback( EndReadCallback ), cState );
   cState->Waiter->WaitOne();
   
   // Finished with the current client.
   authStream->Close();
   clientRequest->Close();
}


public static void AuthenticateClient(TcpClient clientRequest)
{
    NetworkStream stream = clientRequest.GetStream();
    // Create the NegotiateStream.
    NegotiateStream authStream = new NegotiateStream(stream, false);
    // Save the current client and NegotiateStream instance
    // in a ClientState object.
    ClientState cState = new ClientState(authStream, clientRequest);
    // Listen for the client authentication request.
    Task authTask = authStream
        .AuthenticateAsServerAsync()
        .ContinueWith(task => { EndAuthenticateCallback(cState); });

    // Any exceptions that occurred during authentication are
    // thrown by the EndAuthenticateAsServer method.
    try
    {
        // This call blocks until the authentication is complete.
        authTask.Wait();
    }
    catch (AuthenticationException e)
    {
        Console.WriteLine(e);
        Console.WriteLine("Authentication failed - closing connection.");
        return;
    }
    catch (Exception e)
    {
        Console.WriteLine(e);
        Console.WriteLine("Closing connection.");
        return;
    }

    Task<int> readTask = authStream
        .ReadAsync(cState.Buffer, 0, cState.Buffer.Length);

    readTask
        .ContinueWith((task) => { EndReadCallback(cState, task.Result); })
        .Wait();
    // Finished with the current client.
    authStream.Close();
    clientRequest.Close();
}

注釈

暗号化、署名、または暗号化と署名が有効になっている場合、読み取り操作は基になるストリームからデータを読み取り、データの整合性をチェックして暗号化を解除します。 データ暗号化や署名などのセキュリティ サービスが使用されていない場合、このメソッドは基になるストリームで非同期読み取り操作を開始します。

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

非同期読み取り操作は、 メソッドを呼び出して完了する EndRead 必要があります。 通常、 メソッドはデリゲートによって asyncCallback 呼び出されます。 非同期プログラミング モデルの使用の詳細については、「同期メソッドの非同期呼び出し」を参照してください。

クラスは NegotiateStream 、複数の同時読み取り操作をサポートしていません。 同じストリームで別の読み取り操作が既に実行されている間に読み取り操作を開始しようとすると、 NotSupportedException 例外がスローされます。

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

適用対象