語言

NegotiateStream.EndRead(IAsyncResult) 方法

定義

結束一個以呼叫 為 BeginRead(Byte[], Int32, Int32, AsyncCallback, Object)的非同步讀取操作。

public:
 override int EndRead(IAsyncResult ^ asyncResult);
public override int EndRead(IAsyncResult asyncResult);
override this.EndRead : IAsyncResult -> int
Public Overrides Function EndRead (asyncResult As IAsyncResult) As Integer

參數

傳回

一個 Int32 指定從底層串流讀取的位元組數的值。

例外狀況

asyncResult 是 null。

沒有待處理的讀取操作需要完成。

-或-

尚未進行認證。

讀取操作失敗了。

範例

以下程式碼範例示範結束非同步讀取操作。 關於啟動操作的範例,請參見 BeginRead。

private static void EndReadCallback(ClientState cState, int bytes)
{
    NegotiateStream authStream = (NegotiateStream)cState.AuthenticatedStream;
    // Read the client message.
    try
    {
        cState.Message.Append(Encoding.UTF8.GetChars(cState.Buffer, 0, bytes));
        if (bytes != 0)
        {
            Task<int> readTask = authStream.ReadAsync(cState.Buffer, 0, cState.Buffer.Length);
            readTask
                .ContinueWith(task => { EndReadCallback(cState, task.Result); })
                .Wait();

            return;
        }
    }
    catch (Exception e)
    {
        // A real application should do something
        // useful here, such as logging the failure.
        Console.WriteLine("Client message exception:");
        Console.WriteLine(e);
        return;
    }
    IIdentity id = authStream.RemoteIdentity;
    Console.WriteLine("{0} says {1}", id.Name, cState.Message.ToString());
}

備註

如果操作尚未完成,此方法會阻塞直到完成。

要同步執行此操作,請使用該 Read 方法。

在成功驗證之前,你不能呼叫這個方法。 要驗證,請呼叫其中一個 AuthenticateAsClient、 AuthenticateAsClientAsync、 BeginAuthenticateAsClient、 AuthenticateAsServerAuthenticateAsServerAsync、 或 BeginAuthenticateAsServer 方法。

適用於