SslStream.Read(Byte[], Int32, Int32) Metódus

Definíció

Beolvassa az adatokat ebből a streamből, és a megadott tömbben tárolja.

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

Paraméterek

buffer
Byte[]

Egy Byte tömb, amely a streamből beolvasott bájtokat fogadja.

offset
Int32

Az Int32 a nulla alapú hely buffer , ahol a streamből beolvasott adatok tárolása megkezdhető.

count
Int32

A Int32 streamből beolvasható bájtok maximális számát tartalmazó érték.

Válaszok

Az Int32 olvasási bájtok számát meghatározó érték. Ha nincs több beolvasandó adat, 0 értéket ad vissza.

Kivételek

buffer az null.

offset kisebb, mint nulla.

-vagy-

offset nagyobb, mint a hossza buffer.

-vagy-

offset + a szám nagyobb, mint a hossza buffer.

Az olvasási művelet nem sikerült. Ellenőrizze a belső kivételt, ha van ilyen, és állapítsa meg a hiba okát.

Már folyamatban van egy olvasási művelet.

Ezt az objektumot bezárták.

A hitelesítés nem történt meg.

Példák

Az alábbi példakód bemutatja az olvasást egy 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

Megjegyzések

A metódus legfeljebb count bájtokat olvas be a streamből, és azokat a következő időpontban buffer tárolja: offset. Nem hajthat végre egyszerre több olvasási műveletet.

Ezt a metódust csak a sikeres hitelesítés után hívhatja meg. Az egyik metódus BeginAuthenticateAsClientAuthenticateAsServerBeginAuthenticateAsServer meghívásának AuthenticateAsClienthitelesítéséhez.

A művelet aszinkron végrehajtásához használja a metódust BeginRead .

A következőre érvényes: