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 中以零起始的位置,用來開始儲存從此資料流中讀取的資料。

count
Int32

自資料流中讀取的最大位元組數。

asyncCallback
AsyncCallback

AsyncCallback 委派,其會參考讀取作業完成時要叫用的方法。

asyncState
Object

包含讀取作業資訊的使用者定義物件。 作業完成時會將這個物件傳遞給 asyncCallback 委派。

傳回

IAsyncResult 物件,指出非同步作業的狀態。

例外狀況

buffernull

offset 小於 0。

-或-

offset 大於 buffer 的長度。

-或-

offset 加上 count 大於 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 將會擲回例外狀況。

在成功驗證之前,您無法呼叫此方法。 若要進行驗證,請呼叫其中一個 AuthenticateAsClientAuthenticateAsClientAsyncBeginAuthenticateAsClientAuthenticateAsServerAuthenticateAsServerAsyncBeginAuthenticateAsServer 方法。

適用於