NegotiateStream.BeginWrite 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
開始一個非同步寫入操作,從指定的緩衝區寫 Byte入資料到串流。
public:
override IAsyncResult ^ BeginWrite(cli::array <System::Byte> ^ buffer, int offset, int count, AsyncCallback ^ asyncCallback, System::Object ^ asyncState);
public override IAsyncResult BeginWrite(byte[] buffer, int offset, int count, AsyncCallback? asyncCallback, object? asyncState);
public override IAsyncResult BeginWrite(byte[] buffer, int offset, int count, AsyncCallback asyncCallback, object asyncState);
override this.BeginWrite : byte[] * int * int * AsyncCallback * obj -> IAsyncResult
Public Overrides Function BeginWrite (buffer As Byte(), offset As Integer, count As Integer, asyncCallback As AsyncCallback, asyncState As Object) As IAsyncResult
參數
- offset
- Int32
以零為 buffer 基礎的位置,開始讀取要寫入資料串流的位元組。
- asyncCallback
- AsyncCallback
一個 AsyncCallback 代理,當寫入操作完成時會參考要呼叫的方法。
- asyncState
- Object
一個包含寫入操作資訊的使用者定義物件。 當操作完成時,這個物件會傳給代理。asyncCallback
傳回
一個 IAsyncResult 指示非同步操作狀態的物件。
例外狀況
buffer 是 null。
已經有一個寫入操作正在進行中。
這個物件已經關閉了。
尚未進行認證。
範例
以下範例示範開始非同步寫入操作。
// Request authentication.
NetworkStream clientStream = client.GetStream();
NegotiateStream authStream = new NegotiateStream(clientStream, false);
// Pass the NegotiateStream as the AsyncState object
// so that it is available to the callback delegate.
Task authenticateTask = authStream
.AuthenticateAsClientAsync()
.ContinueWith(task =>
{
Console.WriteLine("Client ending authentication...");
Console.WriteLine("ImpersonationLevel: {0}", authStream.ImpersonationLevel);
});
Console.WriteLine("Client waiting for authentication...");
// Wait until the result is available.
authenticateTask.Wait();
// Display the properties of the authenticated stream.
AuthenticatedStreamReporter.DisplayProperties(authStream);
// Send a message to the server.
// Encode the test data into a byte array.
byte[] message = Encoding.UTF8.GetBytes("Hello from the client.");
Task writeTask = authStream
.WriteAsync(message, 0, message.Length)
.ContinueWith(task =>
{
Console.WriteLine("Client ending write operation...");
});
' Request authentication.
Dim clientStream = client.GetStream()
Dim authStream As New NegotiateStream(clientStream, False)
' Pass the NegotiateStream as the AsyncState object
' so that it is available to the callback delegate.
Dim ar = authStream.BeginAuthenticateAsClient(
New AsyncCallback(AddressOf EndAuthenticateCallback), authStream)
Console.WriteLine("Client waiting for authentication...")
' Wait until the result is available.
ar.AsyncWaitHandle.WaitOne()
' Display the properties of the authenticated stream.
AuthenticatedStreamReporter.DisplayProperties(authStream)
' Send a message to the server.
' Encode the test data into a byte array.
Dim message = Encoding.UTF8.GetBytes("Hello from the client.")
ar = authStream.BeginWrite(message, 0, message.Length,
New AsyncCallback(AddressOf EndWriteCallback), authStream)
當操作完成時,會呼叫以下方法。
' The following method is called when the write operation completes.
Public Shared Sub EndWriteCallback(ar As IAsyncResult)
Console.WriteLine("Client ending write operation...")
Dim authStream = CType(ar.AsyncState, NegotiateStream)
' End the asynchronous operation.
authStream.EndWrite(ar)
End Sub
備註
若啟用加密、簽名或加密加簽名,此方法會從緩衝區讀取資料,進行加密、簽署或加密並簽署,並透過底層串流傳輸。 若未使用資料加密或簽章等安全服務,此方法會對底層串流啟動非同步寫入操作。
此方法是非同步的,且在操作完成時不會阻塞。 要阻擋直到操作完成,請使用該 Read 方法。
非同步讀取操作必須透過呼叫該 EndWrite 方法來完成。 通常,該方法由 asyncCallback 委託人執行。 關於使用非同步程式設計模型的詳細資訊,請參見 「非同步呼叫同步方法」
此 NegotiateStream 類別不支援多個同時寫入操作。 如果你嘗試在另一個寫入操作同時啟動,而另一個寫入操作已經在同一串流 NotSupportedException 上執行,將會拋出異常。
在成功驗證之前,你不能呼叫這個方法。 要驗證,請呼叫其中一個 AuthenticateAsClient、 AuthenticateAsClientAsync、 BeginAuthenticateAsClient、 AuthenticateAsServerAuthenticateAsServerAsync、 或 BeginAuthenticateAsServer 方法。