NegotiateStream.BeginRead(Byte[], Int32, Int32, AsyncCallback, Object) 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
開始異步讀取作業,從數據流讀取數據,並將其儲存在指定的陣列中。
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
參數
- offset
- Int32
這是從此串流讀取資料開始儲存的零基位置 buffer 。
- count
- Int32
從串流中讀取的最大位元組數。
- asyncCallback
- AsyncCallback
一個 AsyncCallback 代理,當讀取操作完成時,會參考呼叫的方法。
- asyncState
- Object
一個由使用者定義的物件,包含讀取操作的資訊。 當操作完成時,這個物件會傳給代理。asyncCallback
傳回
一個 IAsyncResult 指示非同步操作狀態的物件。
例外狀況
buffer 是 null。
已經有讀取操作正在進行中。
這個物件已經關閉了。
尚未進行認證。
範例
以下程式碼範例示範啟動非同步讀取操作。 此程式碼範例是本類別更大範例 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、 BeginAuthenticateAsClient、 AuthenticateAsServerAuthenticateAsServerAsync、 或 BeginAuthenticateAsServer 方法。