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);
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 が 0 未満です。

-または-

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

-または-

offset プラス数が bufferの長さを超えています。

書き込み操作に失敗しました。

-または-

暗号化は使用中ですが、データを暗号化できませんでした。

書き込み操作は既に進行中です。

このオブジェクトは閉じられています。

認証が行われません。

次の例では、非同期書き込み操作を開始する方法を示します。

// 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 例外がスローされます。

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

適用対象