SslStream.Read(Byte[], Int32, Int32) 方法

定义

读取此流中的数据并将其存储在指定的数组中。

public:
 override int Read(cli::array <System::Byte> ^ buffer, int offset, int count);
public override int Read (byte[] buffer, int offset, int count);
override this.Read : byte[] * int * int -> int
Public Overrides Function Read (buffer As Byte(), offset As Integer, count As Integer) As Integer

参数

buffer
Byte[]

一个 Byte 数组,该数组接收从此流读取的字节。

offset
Int32

Int32,它包含 buffer 中从零开始的位置,从此处开始存储从此流读取的数据。

count
Int32

Int32,包含要从此流读取的最大字节数。

返回

Int32 值,该值指定读取的字节数。 如果再也没有要读取的数据,将返回 0。

例外

buffernull

offset 小于零。

offset 大于 buffer 的长度。

offset 加上计数大于 buffer 的长度。

读操作失败。 检查内部异常(如果存在)以确定失败的原因。

已存在一个正在执行的读取操作。

此对象已关闭。

未进行身份验证。

示例

下面的代码示例演示如何从 SslStream读取 。

private:
    static String^ ReadMessage(SslStream^ sslStream)
    {
          
        // Read the  message sent by the server.
        // The end of the message is signaled using the
        // "<EOF>" marker.
        array<Byte>^ buffer = gcnew array<Byte>(2048);
        StringBuilder^ messageData = gcnew StringBuilder;
        // Use Decoder class to convert from bytes to UTF8
        // in case a character spans two buffers.
        Encoding^ u8 = Encoding::UTF8;
        Decoder^ decoder = u8->GetDecoder();

        int bytes = -1;
        do
        {
            bytes = sslStream->Read(buffer, 0, buffer->Length);
             
            array<__wchar_t>^ chars = gcnew array<__wchar_t>(
                decoder->GetCharCount(buffer, 0, bytes));
            decoder->GetChars(buffer, 0, bytes, chars, 0);
            messageData->Append(chars);
             
            // Check for EOF.
            if (messageData->ToString()->IndexOf("<EOF>") != -1)
            {
                break;
            }
        }
        while (bytes != 0);

        return messageData->ToString();
    }
static string ReadMessage(SslStream sslStream)
{
    // Read the  message sent by the server.
    // The end of the message is signaled using the
    // "<EOF>" marker.
    byte [] buffer = new byte[2048];
    StringBuilder messageData = new StringBuilder();
    int bytes = -1;
    do
    {
        bytes = sslStream.Read(buffer, 0, buffer.Length);

        // 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,bytes)];
        decoder.GetChars(buffer, 0, bytes, chars,0);
        messageData.Append (chars);
        // Check for EOF.
        if (messageData.ToString().IndexOf("<EOF>") != -1)
        {
            break;
        }
    } while (bytes != 0);

    return messageData.ToString();
}
Private Shared Function ReadMessage(sslStream As SslStream) As String

    ' Read the  message sent by the server.
    ' The end of the message is signaled using the "<EOF>" marker.
    Dim buffer = New Byte(2048) {}
    Dim messageData = New StringBuilder()
    Dim bytes As Integer

    Do
        bytes = sslStream.Read(buffer, 0, buffer.Length)

        ' 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, bytes) - 1) {}
        decoder.GetChars(buffer, 0, bytes, chars, 0)
        messageData.Append(chars)

        ' Check for EOF.
        If messageData.ToString().IndexOf("<EOF>") <> -1 Then Exit Do
        
    Loop While bytes <> 0

    Return messageData.ToString()

End Function

注解

方法从流中读取最大字节数 count ,并将其存储在 buffer 开头 offset。 不能同时执行多个读取操作。

在成功进行身份验证之前,无法调用此方法。 若要进行身份验证,AuthenticateAsClient请调用 、 或 BeginAuthenticateAsClientAuthenticateAsServerBeginAuthenticateAsServer 方法之一。

若要异步执行此操作,请使用 BeginRead 方法。

适用于