SslStream.BeginRead(Byte[], Int32, Int32, AsyncCallback, Object) Metoda
Definice
Důležité
Některé informace platí pro předběžně vydaný produkt, který se může zásadně změnit, než ho výrobce nebo autor vydá. Microsoft neposkytuje žádné záruky, výslovné ani předpokládané, týkající se zde uváděných informací.
Zahájí asynchronní operaci čtení, která čte data z datového proudu a uloží je do zadaného pole.
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
Parametry
- offset
- Int32
Umístění založené na nule, ve buffer kterém chcete začít ukládat data načtená z tohoto datového proudu.
- count
- Int32
Maximální počet bajtů, které se mají číst z datového proudu.
- asyncCallback
- AsyncCallback
AsyncCallback Delegát, který odkazuje na metodu vyvolat při dokončení operace čtení.
- asyncState
- Object
Uživatelem definovaný objekt, který obsahuje informace o operaci čtení. Tento objekt se předá delegátu asyncCallback po dokončení operace.
Návraty
Objekt IAsyncResult , který označuje stav asynchronní operace.
Výjimky
buffer je null.
Hodnota offset je menší než nula.
nebo
offset je větší než délka buffer.
nebo
offset + počet je větší než délka buffer.
Právě probíhá operace čtení.
Tento objekt byl uzavřen.
K ověřování nedošlo.
Příklady
Následující příklad kódu ukazuje spuštění asynchronní operace čtení.
// 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
Následující metoda je volána po dokončení čtení.
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
Poznámky
Pokud je povolené šifrování nebo podepisování, operace čtení načte data z podkladového datového proudu, zkontroluje integritu dat a/nebo je dešifruje. Asynchronní operace čtení musí být dokončena voláním EndRead metody. Obvykle je metoda vyvolána delegátem asyncCallback .
Tato metoda neblokuje, dokud se operace dokončí. Pokud chcete blokovat, dokud se operace neskončí, použijte metodu Read .
Podrobné informace o použití asynchronního programovacího modelu naleznete v tématu Asynchronní volání synchronních metod
Třída SslStream nepodporuje více souběžných operací čtení.
Tuto metodu nelze volat, dokud nebudete úspěšně ověřeni. Ověření volání jedné z AuthenticateAsClientmetod ( nebo BeginAuthenticateAsClient) AuthenticateAsServerBeginAuthenticateAsServer