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 개체입니다.

예외

buffer이(가) null인 경우

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;
    }
}

적용 대상