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 流上执行时尝试启动写入操作,则会引发异常。

在成功进行身份验证之前,无法调用此方法。 若要进行身份验证,请调用 、、AuthenticateAsClientAsyncAuthenticateAsServerAsyncBeginAuthenticateAsClientAuthenticateAsServer、 或 BeginAuthenticateAsServer 方法之一。AuthenticateAsClient

适用于