SslStream.EndRead(IAsyncResult) 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
结束通过以前调用 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
参数
- asyncResult
- IAsyncResult
调用 BeginRead(Byte[], Int32, Int32, AsyncCallback, Object) 后返回的 IAsyncResult 实例。
返回
Int32 值,该值指定从基础流中读取的字节数。
例外
asyncResult
为 null
。
asyncResult
并不是通过对 BeginRead(Byte[], Int32, Int32, AsyncCallback, Object) 的调用创建的。
读操作失败。
示例
下面的代码示例演示如何结束异步读取操作。
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 = dynamic_cast<SslStream^>(ar->AsyncState);
int byteCount = -1;
try
{
Console::WriteLine( L"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();
array<Char>^chars = gcnew array<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( L"<EOF>" ) == -1 && byteCount != 0 )
{
// We are not finished reading.
// Asynchronously read more message data from the server.
stream->BeginRead( buffer, 0, buffer->Length, gcnew AsyncCallback( ReadCallback ), stream );
}
else
{
Console::WriteLine( L"Message from the server: {0}", readData );
}
}
catch ( Exception^ readException )
{
e = readException;
complete = true;
return;
}
complete = true;
}
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
注解
如果操作尚未完成,此方法将一直阻止,直到它完成。
若要同步执行此操作,请使用 Read 方法。
在成功进行身份验证之前,无法调用此方法。 若要进行身份验证,AuthenticateAsClient请调用 、 或 BeginAuthenticateAsClient、 AuthenticateAsServerBeginAuthenticateAsServer 方法之一。