SslStream.Read 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
多載
| 名稱 | Description |
|---|---|
| Read(Span<Byte>) |
在衍生類別中覆寫時,從目前數據流讀取位元組序列,並將數據流中的位置依讀取的位元組數目往前移。 |
| Read(Byte[], Int32, Int32) |
從這個數據流讀取數據,並將它儲存在指定的陣列中。 |
Read(Span<Byte>)
- 來源:
- SslStream.cs
- 來源:
- 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
參數
傳回
讀取到緩衝區的總位元組數。 如果目前沒有那麼多位元組可用,這個值可以小於緩衝區大小;若緩衝區長度為零或已到達串流結束,則可為零(0)。
適用於
Read(Byte[], Int32, Int32)
- 來源:
- SslStream.cs
- 來源:
- 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
參數
傳回
一個 Int32 指定讀取位元組數的值。 當沒有更多資料可讀取時,回傳 0。
例外狀況
buffer 是 null。
讀取操作失敗了。 檢查內部例外(如果有的話),以找出故障原因。
已經有讀取操作正在進行中。
這個物件已經關閉了。
尚未進行認證。
範例
以下程式碼範例示範從 讀取 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 位元組,並從 開始儲存在 bufferoffset。 你不能同時執行多個讀取操作。
在成功驗證之前,你不能呼叫這個方法。 要驗證,呼叫其中一個 AuthenticateAsClient,或 BeginAuthenticateAsClient, AuthenticateAsServer, BeginAuthenticateAsServer 方法。
若要非同步執行此操作,請使用該 BeginRead 方法。