共用方式為


NegotiateStream.BeginWrite 方法

定義

開始非同步寫入作業,此作業會從指定的緩衝區寫入 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

參數

buffer
Byte[]

Byte 陣列,提供將要寫入至資料流的位元組。

offset
Int32

buffer 中以零起始的位置,用來開始讀取將要寫入資料流的位元組。

count
Int32

Int32 值,指定從 buffer 中讀取的位元組數。

asyncCallback
AsyncCallback

AsyncCallback 委派,其會參考寫入作業完成時要叫用的方法。

asyncState
Object

包含寫入作業資訊的使用者定義物件。 作業完成時會將這個物件傳遞給 asyncCallback 委派。

傳回

IAsyncResult 物件,指出非同步作業的狀態。

例外狀況

buffernull

offset is less than 0.

-或-

offset 大於 buffer 的長度。

-或-

offset 加上計數大於 buffer 的長度。

寫入作業失敗。

-或-

使用了加密,但無法解密資料。

已經有寫入作業正在進行中。

此物件已關閉。

尚未執行驗證。

範例

下列範例示範如何開始異步寫入作業。

// Request authentication.
NetworkStream^ clientStream = client->GetStream();
NegotiateStream^ authStream = gcnew NegotiateStream( clientStream,false );

// Pass the NegotiateStream as the AsyncState object 
// so that it is available to the callback delegate.
IAsyncResult^ ar = authStream->BeginAuthenticateAsClient( gcnew AsyncCallback( EndAuthenticateCallback ), authStream );

Console::WriteLine( L"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.
array<Byte>^message = Encoding::UTF8->GetBytes( L"Hello from the client." );
ar = authStream->BeginWrite( message, 0, message->Length, gcnew AsyncCallback( EndWriteCallback ), authStream );

// 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.
static void EndWriteCallback( IAsyncResult^ ar )
{
   Console::WriteLine( L"Client ending write operation..." );
   NegotiateStream^ authStream = dynamic_cast<NegotiateStream^>(ar->AsyncState);
   
   // End the asynchronous operation.
   authStream->EndWrite( ar );
}

' 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一個 、 AuthenticateAsClientAsyncBeginAuthenticateAsClientAuthenticateAsServerAuthenticateAsServerAsync、 或 BeginAuthenticateAsServer 方法。

適用於