SslStream.Write 方法

定义

将数据写入此流。

重载

名称 说明
Write(Byte[])

将指定的数据写入此流。

Write(Byte[], Int32, Int32)

使用指定的缓冲区和偏移量将指定的 s 数 Byte写入基础流。

Write(Byte[])

将指定的数据写入此流。

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 该方法。

在成功进行身份验证之前,无法调用此方法。 若要进行身份验证,请调用其中一种AuthenticateAsClientBeginAuthenticateAsClient方法 BeginAuthenticateAsServerAuthenticateAsServer

SslStream 类不支持多个同时写入操作。

适用于

Write(Byte[], Int32, Int32)

使用指定的缓冲区和偏移量将指定的 s 数 Byte写入基础流。

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

一个 Int32 包含从零开始的位置,从该位置 buffer 开始读取要写入流的字节。

count
Int32

一个 Int32 包含要从 buffer中读取的字节数。

例外

buffernull

offset 小于零。

-或-

offset 大于长度 buffer

-或-

offset + count 大于长度 buffer

写入操作失败。

正在进行写入操作。

此对象已关闭。

身份验证未发生。

注解

此方法在操作完成时阻止。 若要防止在操作完成操作时阻止操作完成,请使用 BeginWrite 该方法。

在成功进行身份验证之前,无法调用此方法。 若要进行身份验证,请调用其中一种AuthenticateAsClientBeginAuthenticateAsClient方法 BeginAuthenticateAsServerAuthenticateAsServer

SslStream 类不支持多个同时写入操作。

适用于