SslStream.BeginRead(Byte[], Int32, Int32, AsyncCallback, Object) Metodo
Definizione
Importante
Alcune informazioni sono relative alla release non definitiva del prodotto, che potrebbe subire modifiche significative prima della release definitiva. Microsoft non riconosce alcuna garanzia, espressa o implicita, in merito alle informazioni qui fornite.
Avvia un'operazione di lettura asincrona dei dati del flusso, archiviandoli nella matrice specificata.
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
Parametri
- offset
- Int32
Posizione in base zero nel buffer da cui iniziare l'archiviazione dei dati letti da questo flusso.
- count
- Int32
Numero massimo di byte da leggere dal flusso.
- asyncCallback
- AsyncCallback
Delegato AsyncCallback cui fa riferimento il metodo da richiamare quando l'operazione di lettura è completa.
- asyncState
- Object
Oggetto definito dall'utente contenente informazioni sull'operazione di lettura. Questo oggetto viene passato al delegato asyncCallback al completamento dell'operazione.
Valori restituiti
Oggetto IAsyncResult che indica lo stato dell'operazione asincrona.
Eccezioni
buffer è null.
offset è minore di zero.
-oppure-
offset è maggiore della lunghezza di buffer.
-oppure-
La somma di offset e conteggio è maggiore della lunghezza del buffer.
L'operazione di lettura non è riuscita.
-oppure-
La crittografia è in uso, ma non è stato possibile decrittografare i dati.
È già in corso un'operazione di lettura.
L'oggetto è stato chiuso.
L'autenticazione non è stata effettuata.
Esempio
Nell'esempio di codice seguente viene illustrato l'avvio di un'operazione di lettura asincrona.
// readData and buffer holds the data read from the server.
// They is used by the ReadCallback method.
static StringBuilder readData = new StringBuilder();
static byte [] buffer = new byte[2048];
' readData and buffer holds the data read from the server.
' They is used by the ReadCallback method.
Shared readData As New StringBuilder()
Shared buffer As Byte() = New Byte(2048) {}
static void WriteCallback(IAsyncResult ar)
{
SslStream stream = (SslStream) ar.AsyncState;
try
{
Console.WriteLine("Writing data to the server.");
stream.EndWrite(ar);
// Asynchronously read a message from the server.
stream.BeginRead(buffer, 0, buffer.Length,
new AsyncCallback(ReadCallback),
stream);
}
catch (Exception writeException)
{
e = writeException;
complete = true;
return;
}
}
Shared Sub WriteCallback(ar As IAsyncResult)
Dim stream = CType(ar.AsyncState, SslStream)
Try
Console.WriteLine("Writing data to the server.")
stream.EndWrite(ar)
' Asynchronously read a message from the server.
stream.BeginRead(buffer, 0, buffer.Length, New AsyncCallback(AddressOf ReadCallback), stream)
Catch writeException As Exception
e = writeException
complete = True
Return
End Try
End Sub
Il metodo seguente viene chiamato al termine della lettura.
static void ReadCallback(IAsyncResult ar)
{
// Read the message sent by the server.
// The end of the message is signaled using the
// "<EOF>" marker.
SslStream stream = (SslStream) ar.AsyncState;
int byteCount = -1;
try
{
Console.WriteLine("Reading data from the server.");
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(buffer,0, byteCount)];
decoder.GetChars(buffer, 0, byteCount, chars,0);
readData.Append (chars);
// Check for EOF or an empty message.
if (readData.ToString().IndexOf("<EOF>") == -1 && byteCount != 0)
{
// We are not finished reading.
// Asynchronously read more message data from the server.
stream.BeginRead(buffer, 0, buffer.Length,
new AsyncCallback(ReadCallback),
stream);
}
else
{
Console.WriteLine("Message from the server: {0}", readData.ToString());
}
}
catch (Exception readException)
{
e = readException;
complete = true;
return;
}
complete = true;
}
Shared Sub ReadCallback(ar As IAsyncResult)
' Read the message sent by the server.
' The end of the message is signaled using the
' "<EOF>" marker.
Dim stream = CType(ar.AsyncState, SslStream)
Dim byteCount As Integer
Try
Console.WriteLine("Reading data from the server.")
byteCount = stream.EndRead(ar)
' Use Decoder class to convert from bytes to UTF8
' in case a character spans two buffers.
Dim decoder As Decoder = Encoding.UTF8.GetDecoder()
Dim chars = New Char(decoder.GetCharCount(buffer, 0, byteCount)) {}
decoder.GetChars(buffer, 0, byteCount, chars, 0)
readData.Append(chars)
' Check for EOF or an empty message.
If readData.ToString().IndexOf("<EOF>") = -1 AndAlso byteCount <> 0 Then
' We are not finished reading.
' Asynchronously read more message data from the server.
stream.BeginRead(buffer, 0, buffer.Length, New AsyncCallback(AddressOf ReadCallback), stream)
Else
Console.WriteLine("Message from the server: {0}", readData.ToString())
End If
Catch readException As Exception
e = readException
complete = True
Return
End Try
complete = True
End Sub
Commenti
Se la crittografia e la firma sono abilitate, l'operazione di lettura legge i dati dal flusso sottostante, controlla l'integrità dei dati e/o la decrittografa. L'operazione di lettura asincrona deve essere completata chiamando il EndRead metodo . In genere, il metodo viene richiamato dal asyncCallback delegato.
Questo metodo non blocca mentre l'operazione viene completata. Per bloccare fino al completamento dell'operazione, usare il Read metodo .
Per informazioni dettagliate sull'uso del modello di programmazione asincrona, vedere Chiamata di metodi sincroni in modo asincrono
La SslStream classe non supporta più operazioni di lettura simultanee.
Non è possibile chiamare questo metodo fino a quando non è stata eseguita correttamente l'autenticazione. Per eseguire l'autenticazioneAuthenticateAsClient, chiamare uno dei metodi , o BeginAuthenticateAsClientAuthenticateAsServer, BeginAuthenticateAsServer .