NegotiateStream.BeginWrite(Byte[], Int32, Int32, AsyncCallback, Object) メソッド

定義

指定したバッファーからストリームに Byte を書き込む非同期書き込み操作を開始します。Begins an asynchronous write operation that writes Bytes from the specified buffer to the stream.

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

パラメーター

buffer
Byte[]

ストリームに書き込むバイトを提供する Byte 配列。A Byte array that supplies the bytes to be written to the stream.

offset
Int32

ストリームに書き込むバイトの読み取りを開始する位置を示す、buffer 内のインデックス番号が 0 から始まる位置。The zero-based location in buffer at which to begin reading bytes to be written to the stream.

count
Int32

Int32 から読み取るバイト数を指定する buffer 値。An Int32 value that specifies the number of bytes to read from buffer.

asyncCallback
AsyncCallback

書き込み操作の完了時に呼び出すメソッドを参照する AsyncCallback デリゲート。An AsyncCallback delegate that references the method to invoke when the write operation is complete.

asyncState
Object

書き込み操作に関する情報を格納するユーザー定義のオブジェクト。A user-defined object containing information about the write operation. このオブジェクトは、操作の完了時に asyncCallback デリゲートに渡されます。This object is passed to the asyncCallback delegate when the operation completes.

戻り値

非同期操作の状態を示す IAsyncResult オブジェクト。An IAsyncResult object indicating the status of the asynchronous operation.

例外

buffernull です。buffer is null.

offset is less than 0 で初期化します。offset is less than 0.

または-or- offsetbuffer の長さを超えています。offset is greater than the length of buffer.

または-or- offset に count を加算した値が、buffer の長さを超えています。offset plus count is greater than the length of buffer.

書き込み操作に失敗しました。The write operation failed.

または-or-

暗号化が使用されていますが、データを暗号化できませんでした。Encryption is in use, but the data could not be encrypted.

既に実行中の書き込み操作が存在します。There is already a write operation in progress.

このオブジェクトは閉じられました。This object has been closed.

認証が行われていません。Authentication has not occurred.

非同期の書き込み操作を開始する例を次に示します。The following example demonstrates beginning an asynchronous write operation.

// 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.
IAsyncResult ar = authStream.BeginAuthenticateAsClient(
    new AsyncCallback(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.
byte[] message = Encoding.UTF8.GetBytes("Hello from the client.");
ar = authStream.BeginWrite(message, 0, message.Length,
    new AsyncCallback(EndWriteCallback),
    authStream);
' 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 operation completes.

// 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 static void EndWriteCallback (IAsyncResult ar)
{
    Console.WriteLine("Client ending write operation...");
    NegotiateStream authStream = (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

注釈

暗号化、署名、または暗号化と署名が有効になっている場合、このメソッドは、バッファーからデータを読み取り、暗号化、署名、または暗号化と署名を行い、基になるストリームを使用して送信します。If encryption, signing, or encryption and signing are enabled, this method reads the data from buffer, encrypts, signs, or encrypts and signs it, and transmits it using the underlying stream. データの暗号化や署名などのセキュリティサービスが使用されていない場合、このメソッドは、基になるストリームに対して非同期の書き込み操作を開始します。If no security services such as data encryption or signing are in use, this method starts an asynchronous write operation on the underlying stream.

このメソッドは非同期であり、操作の完了時にブロックされません。This method is asynchronous and does not block while the operation completes. 操作が完了するまでブロックするには、Read メソッドを使用します。To block until the operation completes, use the Read method.

非同期の読み取り操作は、EndWrite メソッドを呼び出すことによって完了する必要があります。The asynchronous read operation must be completed by calling the EndWrite method. 通常、メソッドは asyncCallback デリゲートによって呼び出されます。Typically, the method is invoked by the asyncCallback delegate. 非同期プログラミングモデルの使用方法の詳細については、「同期メソッドの非同期呼び出し」を参照してください。For detailed information about using the asynchronous programming model, see Calling Synchronous Methods Asynchronously

NegotiateStream クラスは、複数の同時書き込み操作をサポートしていません。The NegotiateStream class does not support multiple simultaneous write operations. 同じストリームで別の書き込み操作が既に実行されているときに書き込み操作を開始しようとすると、NotSupportedException 例外がスローされます。If you attempt to start a write operation while another write operation is already executing on the same stream, a NotSupportedException exception will be thrown.

が正常に認証されるまで、このメソッドを呼び出すことはできません。You cannot call this method until you have successfully authenticated. 認証するには、AuthenticateAsClientBeginAuthenticateAsClientAuthenticateAsServer、または BeginAuthenticateAsServer メソッドのいずれかを呼び出します。To authenticate, call one of the AuthenticateAsClient, BeginAuthenticateAsClient, AuthenticateAsServer, or BeginAuthenticateAsServer methods.

適用対象