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 内のインデックス番号が 0 から始まる位置。

count
Int32

buffer から読み取るバイト数を指定する Int32 値。

asyncCallback
AsyncCallback

書き込み操作の完了時に呼び出すメソッドを参照する AsyncCallback デリゲート。

asyncState
Object

書き込み操作に関する情報を格納するユーザー定義のオブジェクト。 このオブジェクトは、操作の完了時に asyncCallback デリゲートに渡されます。

戻り値

非同期操作の状態を示す IAsyncResult オブジェクト。

例外

buffernullです。

offset is less than 0.

- または -

offsetbuffer の長さを超えています。

- または -

offset に count を加算した値が、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 例外がスローされます。

正常に認証されるまで、このメソッドを呼び出すことはできません。 認証するには、、BeginAuthenticateAsClientAuthenticateAsServerAuthenticateAsServerAsync、または BeginAuthenticateAsServer のいずれかのAuthenticateAsClientAuthenticateAsClientAsyncメソッドを呼び出します。

適用対象