NegotiateStream.BeginWrite Metoda

Definicja

Rozpoczyna asynchroniczną operację zapisu, która zapisuje Bytes z określonego buforu do strumienia.

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

Parametry

buffer
Byte[]

Tablica dostarczająca Byte bajty, które mają być zapisywane w strumieniu.

offset
Int32

Lokalizacja oparta na zerach, w buffer której należy rozpocząć odczytywanie bajtów, które mają być zapisywane w strumieniu.

count
Int32

Wartość Int32 określająca liczbę bajtów do odczytu z bufferelementu .

asyncCallback
AsyncCallback

Delegat AsyncCallback , który odwołuje się do metody wywoływania po zakończeniu operacji zapisu.

asyncState
Object

Obiekt zdefiniowany przez użytkownika zawierający informacje o operacji zapisu. Ten obiekt jest przekazywany do delegata po zakończeniu asyncCallback operacji.

Zwraca

IAsyncResult Obiekt wskazujący stan operacji asynchronicznej.

Wyjątki

buffer to null.

offset is less than 0.

-lub-

offset jest większa niż długość obiektu buffer.

-lub-

offsetplus liczba jest większa niż długość .buffer

Operacja zapisu nie powiodła się.

-lub-

Szyfrowanie jest używane, ale nie można zaszyfrować danych.

Trwa już operacja zapisu.

Ten obiekt został zamknięty.

Uwierzytelnianie nie wystąpiło.

Przykłady

W poniższym przykładzie pokazano rozpoczęcie operacji zapisu asynchronicznego.

// 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)

Następująca metoda jest wywoływana po zakończeniu operacji.

// 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

Uwagi

Jeśli włączono szyfrowanie, podpisywanie lub szyfrowanie i podpisywanie, ta metoda odczytuje dane z buforu, szyfruje, podpisuje lub szyfruje i podpisuje je oraz przesyła je przy użyciu bazowego strumienia. Jeśli nie są używane żadne usługi zabezpieczeń, takie jak szyfrowanie danych lub logowanie, ta metoda uruchamia asynchroniczną operację zapisu w strumieniu bazowym.

Ta metoda jest asynchroniczna i nie blokuje się podczas wykonywania operacji. Aby zablokować działanie do momentu zakończenia operacji, użyj Read metody .

Operacja odczytu asynchronicznego musi zostać ukończona przez wywołanie EndWrite metody . Zazwyczaj metoda jest wywoływana przez delegata asyncCallback . Aby uzyskać szczegółowe informacje o korzystaniu z asynchronicznego modelu programowania, zobacz Wywoływanie metod synchronicznych asynchronicznie

Klasa NegotiateStream nie obsługuje wielu równoczesnych operacji zapisu. Jeśli spróbujesz uruchomić operację zapisu, gdy inna operacja zapisu jest już wykonywana w tym samym strumieniu, NotSupportedException zostanie zgłoszony wyjątek.

Nie można wywołać tej metody do momentu pomyślnego uwierzytelnienia. Aby przeprowadzić uwierzytelnianie, wywołaj jedną z AuthenticateAsClientmetod , , AuthenticateAsClientAsyncAuthenticateAsServerBeginAuthenticateAsClient, AuthenticateAsServerAsynclub .BeginAuthenticateAsServer

Dotyczy