Export (0) Print
Expand All

SslStream::BeginWrite Method

Begins an asynchronous write operation that writes Bytes from the specified buffer to the stream.

Namespace:  System.Net.Security
Assembly:  System (in System.dll)

[HostProtectionAttribute(SecurityAction::LinkDemand, ExternalThreading = true)]
public:
virtual IAsyncResult^ BeginWrite(
	array<unsigned char>^ buffer, 
	int offset, 
	int count, 
	AsyncCallback^ asyncCallback, 
	Object^ asyncState
) override

Parameters

buffer
Type: array<System::Byte>

A Byte array that supplies the bytes to be written to the stream.

offset
Type: System::Int32

The zero-based location in buffer at which to begin reading bytes to be written to the stream.

count
Type: System::Int32

An Int32 value that specifies the number of bytes to read from buffer.

asyncCallback
Type: System::AsyncCallback

An AsyncCallback delegate that references the method to invoke when the write operation is complete.

asyncState
Type: System::Object

A user-defined object that contains information about the write operation. This object is passed to the asyncCallback delegate when the operation completes.

Return Value

Type: System::IAsyncResult
An IAsyncResult object indicating the status of the asynchronous operation.

ExceptionCondition
ArgumentNullException

buffer is nullptr.

ArgumentException

offset < 0.

-or-

offset > the length of buffer.

-or-

offset + count > the length of buffer.

IOException

The write operation failed.

NotSupportedException

There is already a write operation in progress.

ObjectDisposedException

This object has been closed.

InvalidOperationException

Authentication has not occurred.

NoteNote

The HostProtectionAttribute attribute applied to this type or member has the following Resources property value: ExternalThreading. The HostProtectionAttribute does not affect desktop applications (which are typically started by double-clicking an icon, typing a command, or entering a URL in a browser). For more information, see the HostProtectionAttribute class or SQL Server Programming and Host Protection Attributes.

The following code example demonstrates calling this method.

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

   }

.NET Framework

Supported in: 4.5, 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft