Share via


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

在成功进行身份验证之前,无法调用此方法。 若要进行身份验证,请调用 、AuthenticateAsClientAsync、、BeginAuthenticateAsClientAuthenticateAsServerAuthenticateAsServerAsyncBeginAuthenticateAsServer 方法之AuthenticateAsClient一。

适用于