共用方式為


SslStream.Read 方法

定義

多載

Read(Span<Byte>)
Read(Byte[], Int32, Int32)

從這個資料流中讀取資料並將其儲存於指定的陣列中。

Read(Span<Byte>)

來源:
SslStream.cs
來源:
SslStream.cs
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

參數

buffer
Span<Byte>

傳回

適用於

Read(Byte[], Int32, Int32)

來源:
SslStream.cs
來源:
SslStream.cs
來源:
SslStream.cs
來源:
SslStream.cs

從這個資料流中讀取資料並將其儲存於指定的陣列中。

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讀取。

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並從 開始offset儲存它們buffer。 您無法執行多個同時讀取作業。

在成功驗證之前,您無法呼叫這個方法。 若要驗證,請呼叫其中AuthenticateAsClient一個 、 或 BeginAuthenticateAsClientAuthenticateAsServerBeginAuthenticateAsServer 方法。

若要以異步方式執行這項作業,請使用 BeginRead 方法。

適用於