SslStream.Read Metodo

Definizione

Overload

Nome Descrizione
Read(Span<Byte>)

Quando sottoposto a override in una classe derivata, legge una sequenza di byte dal flusso corrente e sposta in avanti la posizione all'interno del flusso in base al numero di byte letti.

Read(Byte[], Int32, Int32)

Legge i dati da questo flusso e li archivia nella matrice specificata.

Read(Span<Byte>)

Origine:
SslStream.cs
Origine:
SslStream.cs
Origine:
SslStream.cs

Quando sottoposto a override in una classe derivata, legge una sequenza di byte dal flusso corrente e sposta in avanti la posizione all'interno del flusso in base al numero di byte letti.

public:
 override int Read(Span<System::Byte> buffer);
public override int Read(Span<byte> buffer);
override this.Read : Span<byte> -> int
Public Overrides Function Read (buffer As Span(Of Byte)) As Integer

Parametri

buffer
Span<Byte>

Area di memoria. Quando termina, il contenuto di questa area viene sostituito dai byte letti dall'origine corrente.

Valori restituiti

Numero totale di byte letti nel buffer. Può essere inferiore alla dimensione del buffer se molti byte non sono attualmente disponibili oppure zero (0) se la lunghezza del buffer è zero o la fine del flusso è stata raggiunta.

Si applica a

Read(Byte[], Int32, Int32)

Origine:
SslStream.cs
Origine:
SslStream.cs
Origine:
SslStream.cs
Origine:
SslStream.cs
Origine:
SslStream.cs

Legge i dati da questo flusso e li archivia nella matrice specificata.

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

Parametri

buffer
Byte[]

Matrice Byte che riceve i byte letti da questo flusso.

offset
Int32

Oggetto Int32 contenente la posizione in base zero in in buffer cui iniziare a archiviare i dati letti da questo flusso.

count
Int32

Oggetto Int32 contenente il numero massimo di byte da leggere da questo flusso.

Valori restituiti

Valore Int32 che specifica il numero di byte letti. Quando non sono presenti altri dati da leggere, restituisce 0.

Eccezioni

buffer è null.

offset è minore di zero.

oppure

offset è maggiore della lunghezza di buffer.

oppure

offset + count è maggiore della lunghezza di buffer.

Operazione di lettura non riuscita. Controllare l'eccezione interna, se presente per determinare la causa dell'errore.

È già in corso un'operazione di lettura.

Questo oggetto è stato chiuso.

L'autenticazione non è stata eseguita.

Esempio

Nell'esempio di codice seguente viene illustrata la lettura da un oggetto 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

Commenti

Il metodo legge un massimo di count byte dal flusso e li archivia a buffer partire da offset. Non è possibile eseguire più operazioni di lettura simultanee.

Non è possibile chiamare questo metodo fino a quando non è stata eseguita correttamente l'autenticazione. Per autenticare chiamare uno dei AuthenticateAsClientmetodi , o BeginAuthenticateAsClient, AuthenticateAsServer. BeginAuthenticateAsServer

Per eseguire questa operazione in modo asincrono, usare il BeginRead metodo .

Si applica a