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
IAsyncResult 執行個體,由對 BeginRead(Byte[], Int32, Int32, AsyncCallback, Object) 的呼叫所傳回。
傳回
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 方法。