Sdílet prostřednictvím


SslStream.Read(Byte[], Int32, Int32) Metoda

Definice

Načte data z tohoto datového proudu a uloží je do zadaného pole.

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

Parametry

buffer
Byte[]

Pole Byte , které přijímá bajty načtené z tohoto datového proudu.

offset
Int32

A Int32 , který obsahuje 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

A Int32 obsahující maximální počet bajtů, které se mají číst z tohoto datového proudu.

Návraty

Hodnota Int32 , která určuje počet přečtených bajtů. Pokud nejsou k dispozici žádná další data ke čtení, vrátí 0.

Výjimky

buffer je null.

Hodnota offset je menší než nula.

-nebo-

offsetje větší než délka .buffer

-nebo-

offset+count je větší než délka .buffer

Operace čtení se nezdařila. Zkontrolujte vnitřní výjimku, pokud je k dispozici, a určete příčinu selhání.

Operace čtení už probíhá.

Tento objekt byl uzavřen.

K ověření nedošlo.

Příklady

Následující příklad kódu ukazuje čtení z objektu SslStream.

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

Poznámky

Metoda načte z datového proudu maximálně bajty count a ukládá je od bufferoffset. Nelze provést více souběžných operací čtení.

Tuto metodu nelze volat, dokud se úspěšně neověříte. K ověření zavolejte některou AuthenticateAsClientz metod , nebo BeginAuthenticateAsClient, AuthenticateAsServer. BeginAuthenticateAsServer

Pokud chcete tuto operaci provést asynchronně, použijte metodu BeginRead .

Platí pro