SslStream.Write 方法

定義

將資料寫入這條串流。

多載

名稱 Description
Write(Byte[])

將指定的數據寫入此數據流。

Write(ReadOnlySpan<Byte>)

在衍生類別中覆寫時,將位元組序列寫入目前數據流,並依寫入的位元組數目將這個數據流中的目前位置往前移。

Write(Byte[], Int32, Int32)

利用指定的緩衝區和偏移量,將指定數量 Byte的 s 寫入底層串流。

Write(Byte[])

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

將指定的數據寫入此數據流。

public:
 void Write(cli::array <System::Byte> ^ buffer);
public void Write(byte[] buffer);
override this.Write : byte[] -> unit
Public Sub Write (buffer As Byte())

參數

buffer
Byte[]

一個 Byte 陣列,用來提供寫入串流的位元組。

例外狀況

buffernull

寫入操作失敗了。

已經有一個寫入操作正在進行中。

這個物件已經關閉了。

尚未進行認證。

範例

以下程式碼範例示範了對已認證 SslStream的 。

static void ProcessClient (TcpClient client)
{
    // A client has connected. Create the
    // SslStream using the client's network stream.
    SslStream sslStream = new SslStream(
        client.GetStream(), false);
    // Authenticate the server but don't require the client to authenticate.
    try
    {
        sslStream.AuthenticateAsServer(serverCertificate, clientCertificateRequired: false, checkCertificateRevocation: true);

        // Display the properties and settings for the authenticated stream.
        DisplaySecurityLevel(sslStream);
        DisplaySecurityServices(sslStream);
        DisplayCertificateInformation(sslStream);
        DisplayStreamProperties(sslStream);

        // Set timeouts for the read and write to 5 seconds.
        sslStream.ReadTimeout = 5000;
        sslStream.WriteTimeout = 5000;
        // Read a message from the client.
        Console.WriteLine("Waiting for client message...");
        string messageData = ReadMessage(sslStream);
        Console.WriteLine("Received: {0}", messageData);

        // Write a message to the client.
        byte[] message = Encoding.UTF8.GetBytes("Hello from the server.<EOF>");
        Console.WriteLine("Sending hello message.");
        sslStream.Write(message);
    }
    catch (AuthenticationException e)
    {
        Console.WriteLine("Exception: {0}", e.Message);
        if (e.InnerException != null)
        {
            Console.WriteLine("Inner exception: {0}", e.InnerException.Message);
        }
        Console.WriteLine ("Authentication failed - closing the connection.");
        sslStream.Close();
        client.Close();
        return;
    }
    finally
    {
        // The client stream will be closed with the sslStream
        // because we specified this behavior when creating
        // the sslStream.
        sslStream.Close();
        client.Close();
    }
}
Private Shared Sub ProcessClient(client As TcpClient)
    ' A client has connected. Create the 
    ' SslStream using the client's network stream.
    Dim sslStream = New SslStream(client.GetStream(), False)

    Try

        sslStream.AuthenticateAsServer(serverCertificate, clientCertificateRequired:=False, checkCertificateRevocation:=True)
        ' Display the properties And settings for the authenticated stream.
        DisplaySecurityLevel(sslStream)
        DisplaySecurityServices(sslStream)
        DisplayCertificateInformation(sslStream)
        DisplayStreamProperties(sslStream)

        ' Set timeouts for the read and write to 5 seconds.
        sslStream.ReadTimeout = 5000
        sslStream.WriteTimeout = 5000

        ' Read a message from the client.   
        Console.WriteLine("Waiting for client message...")
        Dim messageData As String = ReadMessage(sslStream)
        Console.WriteLine("Received: {0}", messageData)

        ' Write a message to the client.
        Dim message As Byte() = Encoding.UTF8.GetBytes("Hello from the server.<EOF>")
        Console.WriteLine("Sending hello message.")
        sslStream.Write(message)
    Catch e As AuthenticationException
        Console.WriteLine("Exception: {0}", e.Message)

        If e.InnerException IsNot Nothing Then
            Console.WriteLine("Inner exception: {0}", e.InnerException.Message)
        End If

        Console.WriteLine("Authentication failed - closing the connection.")
        sslStream.Close()
        client.Close()
        Return
    Finally
        ' The client stream will be closed with the sslStream
        ' because we specified this behavior when creating
        ' the sslStream.
        sslStream.Close()
        client.Close()
    End Try
End Sub

備註

此方法在操作完成時阻塞。 為了防止操作完成時阻塞,請使用此 BeginWrite 方法。

在成功驗證之前,你不能呼叫這個方法。 要驗證,呼叫其中一個 AuthenticateAsClient,或 BeginAuthenticateAsClientAuthenticateAsServerBeginAuthenticateAsServer 方法。

SslStream 類別不支援多個同時寫入操作。

適用於

Write(ReadOnlySpan<Byte>)

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

在衍生類別中覆寫時,將位元組序列寫入目前數據流,並依寫入的位元組數目將這個數據流中的目前位置往前移。

public:
 override void Write(ReadOnlySpan<System::Byte> buffer);
public override void Write(ReadOnlySpan<byte> buffer);
override this.Write : ReadOnlySpan<byte> -> unit
Public Overrides Sub Write (buffer As ReadOnlySpan(Of Byte))

參數

buffer
ReadOnlySpan<Byte>

一個記憶區域。 此方法會將該區域的內容複製到目前的串流中。

適用於

Write(Byte[], Int32, Int32)

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

利用指定的緩衝區和偏移量,將指定數量 Byte的 s 寫入底層串流。

public:
 override void Write(cli::array <System::Byte> ^ buffer, int offset, int count);
public override void Write(byte[] buffer, int offset, int count);
override this.Write : byte[] * int * int -> unit
Public Overrides Sub Write (buffer As Byte(), offset As Integer, count As Integer)

參數

buffer
Byte[]

一個 Byte 陣列,用來提供寫入串流的位元組。

offset
Int32

A Int32 包含以零為 buffer 基礎的位置,從此開始讀取要寫入串流的位元組。

count
Int32

Int32 A 包含從 中讀取buffer的位元組數。

例外狀況

buffernull

offset 小於零。

-或-

offset 大於 的 buffer長度。

-或-

offset + 計數大於 的 buffer長度。

寫入操作失敗了。

已經有一個寫入操作正在進行中。

這個物件已經關閉了。

尚未進行認證。

備註

此方法在操作完成時阻塞。 為了防止在操作完成時阻塞,請使用以下 BeginWrite 方法。

在成功驗證之前,你不能呼叫這個方法。 要驗證,呼叫其中一個 AuthenticateAsClient,或 BeginAuthenticateAsClientAuthenticateAsServerBeginAuthenticateAsServer 方法。

SslStream 類別不支援多個同時寫入操作。

適用於