SslStream.EndRead(IAsyncResult) Methode
Definitie
Belangrijk
Bepaalde informatie heeft betrekking op een voorlopige productversie die aanzienlijk kan worden gewijzigd voordat deze wordt uitgebracht. Microsoft biedt geen enkele expliciete of impliciete garanties met betrekking tot de informatie die hier wordt verstrekt.
Hiermee wordt een asynchrone leesbewerking beëindigd die is gestart met een vorige aanroep naar BeginRead(Byte[], Int32, Int32, AsyncCallback, Object).
public:
override int EndRead(IAsyncResult ^ asyncResult);
public override int EndRead(IAsyncResult asyncResult);
override this.EndRead : IAsyncResult -> int
Public Overrides Function EndRead (asyncResult As IAsyncResult) As Integer
Parameters
- asyncResult
- IAsyncResult
Een IAsyncResult exemplaar dat wordt geretourneerd door een aanroep naar BeginRead(Byte[], Int32, Int32, AsyncCallback, Object).
Retouren
Een Int32 waarde die het aantal bytes aangeeft dat uit de onderliggende stroom is gelezen.
Uitzonderingen
asyncResult is null.
asyncResult is niet gemaakt door een aanroep naar BeginRead(Byte[], Int32, Int32, AsyncCallback, Object).
Er is geen leesbewerking in behandeling om te voltooien.
– of –
Er is geen verificatie opgetreden.
De leesbewerking is mislukt.
Voorbeelden
In het volgende codevoorbeeld ziet u hoe u een asynchrone leesbewerking beëindigt.
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
Opmerkingen
Als de bewerking niet is voltooid, wordt deze methode geblokkeerd totdat de bewerking is voltooid.
Gebruik de Read methode om deze bewerking synchroon uit te voeren.
U kunt deze methode pas aanroepen als u bent geverifieerd. Als u wilt verifiëren, roept u een van de AuthenticateAsClientmethoden , of BeginAuthenticateAsClient, AuthenticateAsServeraan BeginAuthenticateAsServer .