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、、BeginAuthenticateAsClientAuthenticateAsServer、AuthenticateAsServerAsync或BeginAuthenticateAsServer方法。