Edit

Share via


NegotiateStream.BeginRead(Byte[], Int32, Int32, AsyncCallback, Object) Method

Definition

Begins an asynchronous read operation that reads data from the stream and stores it in the specified array.

public:
 override IAsyncResult ^ BeginRead(cli::array <System::Byte> ^ buffer, int offset, int count, AsyncCallback ^ asyncCallback, System::Object ^ asyncState);
public override IAsyncResult BeginRead (byte[] buffer, int offset, int count, AsyncCallback? asyncCallback, object? asyncState);
public override IAsyncResult BeginRead (byte[] buffer, int offset, int count, AsyncCallback asyncCallback, object asyncState);
override this.BeginRead : byte[] * int * int * AsyncCallback * obj -> IAsyncResult
Public Overrides Function BeginRead (buffer As Byte(), offset As Integer, count As Integer, asyncCallback As AsyncCallback, asyncState As Object) As IAsyncResult

Parameters

buffer
Byte[]

A Byte array that receives the bytes read from the stream.

offset
Int32

The zero-based location in buffer at which to begin storing the data read from this stream.

count
Int32

The maximum number of bytes to read from the stream.

asyncCallback
AsyncCallback

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

asyncState
Object

A user-defined object containing information about the read operation. This object is passed to the asyncCallback delegate when the operation completes.

Returns

An IAsyncResult object indicating the status of the asynchronous operation.

Exceptions

buffer is null.

offset is less than 0.

-or-

offset is greater than the length of buffer.

-or-

offset plus count is greater than the length of buffer.

The read operation failed.

-or-

Encryption is in use, but the data could not be decrypted.

There is already a read operation in progress.

This object has been closed.

Authentication has not occurred.

Examples

The following code example demonstrates starting an asynchronous read operation. This code example is part of a larger example provided for the NegotiateStream class.

static void AuthenticateClient( TcpClient^ clientRequest )
{
   NetworkStream^ stream = clientRequest->GetStream();
   
   // Create the NegotiateStream.
   NegotiateStream^ authStream = gcnew NegotiateStream( stream,false );
   
   // Save the current client and NegotiateStream instance 
   // in a ClientState object.
   ClientState^ cState = gcnew ClientState( authStream,clientRequest );
   
   // Listen for the client authentication request.
   authStream->BeginAuthenticateAsServer( gcnew AsyncCallback( EndAuthenticateCallback ), cState );
   
   // Wait until the authentication completes.
   cState->Waiter->WaitOne();
   cState->Waiter->Reset();
   authStream->BeginRead( cState->Buffer, 0, cState->Buffer->Length, gcnew AsyncCallback( EndReadCallback ), cState );
   cState->Waiter->WaitOne();
   
   // Finished with the current client.
   authStream->Close();
   clientRequest->Close();
}


public static void AuthenticateClient(TcpClient clientRequest)
{
    NetworkStream stream = clientRequest.GetStream();
    // Create the NegotiateStream.
    NegotiateStream authStream = new NegotiateStream(stream, false);
    // Save the current client and NegotiateStream instance
    // in a ClientState object.
    ClientState cState = new ClientState(authStream, clientRequest);
    // Listen for the client authentication request.
    Task authTask = authStream
        .AuthenticateAsServerAsync()
        .ContinueWith(task => { EndAuthenticateCallback(cState); });

    // Any exceptions that occurred during authentication are
    // thrown by the EndAuthenticateAsServer method.
    try
    {
        // This call blocks until the authentication is complete.
        authTask.Wait();
    }
    catch (AuthenticationException e)
    {
        Console.WriteLine(e);
        Console.WriteLine("Authentication failed - closing connection.");
        return;
    }
    catch (Exception e)
    {
        Console.WriteLine(e);
        Console.WriteLine("Closing connection.");
        return;
    }

    Task<int> readTask = authStream
        .ReadAsync(cState.Buffer, 0, cState.Buffer.Length);

    readTask
        .ContinueWith((task) => { EndReadCallback(cState, task.Result); })
        .Wait();
    // Finished with the current client.
    authStream.Close();
    clientRequest.Close();
}

Remarks

If encryption, signing, or encryption and signing are enabled, the read operation reads the data from the underlying stream, checks the integrity of the data, and decrypts it. If no security services such as data encryption or signing are in use, this method starts an asynchronous read operation on the underlying stream.

This method is asynchronous and does not block while the operation completes. To block until the operation completes, use the Read method.

The asynchronous read operation must be completed by calling the EndRead method. Typically, the method is invoked by the asyncCallback delegate. For detailed information about using the asynchronous programming model, see Calling Synchronous Methods Asynchronously

The NegotiateStream class does not support multiple simultaneous read operations. If you attempt to start a read operation while another read operation is already executing on the same stream, a NotSupportedException exception will be thrown.

You cannot call this method until you have successfully authenticated. To authenticate, call one of the AuthenticateAsClient, AuthenticateAsClientAsync, BeginAuthenticateAsClient, AuthenticateAsServer, AuthenticateAsServerAsync, or BeginAuthenticateAsServer methods.

Applies to