语言

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 对象,指示异步操作的状态。

例外

buffer 是 null。

offset 小于 0。

-或-

offset 大于长度 buffer。

-或-

offset 加 count 号大于长度 buffer。

读取操作失败。

-或-

加密正在使用中,但无法解密数据。

正在进行读取操作。

此对象已关闭。

身份验证未发生。

示例

下面的代码示例演示如何启动异步读取操作。 该代码示例是 NegotiateStream 类中的一个较大示例的一部分。

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 则会引发异常。

在成功进行身份验证之前,无法调用此方法。 若要进行身份验证,请调用其中一种AuthenticateAsClient、AuthenticateAsClientAsync、、BeginAuthenticateAsClientAuthenticateAsServer、AuthenticateAsServerAsync或BeginAuthenticateAsServer方法。

适用于