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

定義

指定したバッファーからストリームに 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 が 0 未満です。

または

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

または

offset に count を加算した値が、buffer の長さを超えています。

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

既に実行中の書き込み操作が存在します。

このオブジェクトは閉じられました。

認証が行われていません。

次のコード例では、このメソッドの呼び出しを示します。

void ReadCallback( IAsyncResult^ ar )
{
   ClientState^ state = dynamic_cast<ClientState^>(ar->AsyncState);
   SslStream^ stream = state->stream;
   
   // Read the  message sent by the client.
   // The end of the message is signaled using the
   // "<EOF>" marker.
   int byteCount = -1;
   try
   {
      Console::WriteLine( L"Reading data from the client." );
      byteCount = stream->EndRead( ar );
      
      // Use Decoder class to convert from bytes to UTF8
      // in case a character spans two buffers.
      Decoder^ decoder = Encoding::UTF8->GetDecoder();
      array<Char>^chars = gcnew array<Char>(decoder->GetCharCount( state->buffer, 0, byteCount ));
      decoder->GetChars( state->buffer, 0, byteCount, chars, 0 );
      state->readData->Append( chars );
      
      // Check for EOF or an empty message.
      if ( state->readData->ToString()->IndexOf( L"<EOF>" ) == -1 && byteCount != 0 )
      {
         
         // We are not finished reading.
         // Asynchronously read more message data from  the client.
         stream->BeginRead( state->buffer, 0, state->buffer->Length, gcnew AsyncCallback( this, &SslTcpListener::ReadCallback ), state );
      }
      else
      {
         Console::WriteLine( L"Message from the client: {0}", state->readData );
      }
      
      // Encode a test message into a byte array.
      // Signal the end of the message using "<EOF>".
      array<Byte>^message = Encoding::UTF8->GetBytes( L"Hello from the server.<EOF>" );
      
      // Asynchronously send the message to the client.
      stream->BeginWrite( message, 0, message->Length, gcnew AsyncCallback( this, &SslTcpListener::WriteCallback ), state );
   }
   catch ( Exception^ readException ) 
   {
      Console::WriteLine( L"Read error: {0}", readException->Message );
      state->Close();
      return;
   }

}
void ReadCallback(IAsyncResult ar)
{
    ClientState state = (ClientState) ar.AsyncState;
    SslStream stream = state.stream;
    // Read the  message sent by the client.
    // The end of the message is signaled using the
    // "<EOF>" marker.
    int byteCount = -1;
    try
    {
        Console.WriteLine("Reading data from the client.");
        byteCount = stream.EndRead(ar);
        // Use Decoder class to convert from bytes to UTF8
        // in case a character spans two buffers.
        Decoder decoder = Encoding.UTF8.GetDecoder();
        char[] chars = new char[decoder.GetCharCount(state.buffer,0, byteCount)];
        decoder.GetChars(state.buffer, 0, byteCount, chars,0);
        state.readData.Append (chars);
        // Check for EOF or an empty message.
        if (state.readData.ToString().IndexOf("<EOF>") == -1 && byteCount != 0)
        {
            // We are not finished reading.
            // Asynchronously read more message data from  the client.
            stream.BeginRead(state.buffer, 0, state.buffer.Length,
                new AsyncCallback(ReadCallback),
                state);
        }
        else
        {
            Console.WriteLine("Message from the client: {0}", state.readData.ToString());
        }

        // Encode a test message into a byte array.
        // Signal the end of the message using "<EOF>".
        byte[] message = Encoding.UTF8.GetBytes("Hello from the server.<EOF>");
        // Asynchronously send the message to the client.
        stream.BeginWrite(message, 0, message.Length,
            new AsyncCallback(WriteCallback),
            state);
    }
    catch (Exception readException)
    {
        Console.WriteLine("Read error: {0}", readException.Message);
        state.Close();
        return;
    }
}

適用対象