SslStream.Write Método

Definición

Escribe datos en esta secuencia.Writes data to this stream.

Sobrecargas

Write(Byte[])

Escribe los datos especificados en esta secuencia.Writes the specified data to this stream.

Write(Byte[], Int32, Int32)

Escriba el número especificado de Bytes en la secuencia subyacente mediante el búfer y el desplazamiento especificados.Write the specified number of Bytes to the underlying stream using the specified buffer and offset.

Write(Byte[])

Escribe los datos especificados en esta secuencia.Writes the specified data to this stream.

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

Parámetros

buffer
Byte[]

Matriz de Byte que proporciona los bytes escritos en la secuencia.A Byte array that supplies the bytes written to the stream.

Excepciones

buffer es null.buffer is null.

No se pudo realizar la operación de escritura.The write operation failed.

Ya hay una operación de escritura en curso.There is already a write operation in progress.

Este objeto se ha cerrado.This object has been closed.

No se ha producido la autenticación.Authentication has not occurred.

Ejemplos

En el ejemplo de código siguiente se muestra cómo escribir en un autenticado SslStream .The following code example demonstrates writing to an authenticated SslStream.

static void ProcessClient( TcpClient^ client )
{
   
   // A client has connected. Create the 
   // SslStream using the client's network stream.
   SslStream^ sslStream = gcnew SslStream( client->GetStream(),false );
   
   // Authenticate the server but don't require the client to authenticate.
   try
   {
      sslStream->AuthenticateAsServer( serverCertificate, false, true );
      // false == no client cert required; true == check cert revocation.
      
      // 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( L"Waiting for client message..." );
      String^ messageData = ReadMessage( sslStream );
      Console::WriteLine( L"Received: {0}", messageData );
      
      // Write a message to the client.
      array<Byte>^message = Encoding::UTF8->GetBytes( L"Hello from the server.<EOF>" );
      Console::WriteLine( L"Sending hello message." );
      sslStream->Write( message );
   }
   catch ( AuthenticationException^ e ) 
   {
      Console::WriteLine( L"Exception: {0}", e->Message );
      if ( e->InnerException != nullptr )
      {
         Console::WriteLine( L"Inner exception: {0}", e->InnerException->Message );
      }
      Console::WriteLine( L"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();
   }

}


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

Comentarios

Este método se bloquea mientras se completa la operación.This method blocks while the operation completes. Para evitar el bloqueo mientras se completa la operación, use el BeginWrite método.To prevent blocking while the operation completes, use the BeginWrite method.

No se puede llamar a este método hasta que se haya autenticado correctamente.You cannot call this method until you have successfully authenticated. Para autenticar, llame a uno de los AuthenticateAsClient métodos, o BeginAuthenticateAsClient , AuthenticateAsServer BeginAuthenticateAsServer .To authenticate call one of the AuthenticateAsClient, or BeginAuthenticateAsClient, AuthenticateAsServer, BeginAuthenticateAsServer methods.

La SslStream clase no admite varias operaciones de escritura simultáneas.The SslStream class does not support multiple simultaneous write operations.

Se aplica a

Write(Byte[], Int32, Int32)

Escriba el número especificado de Bytes en la secuencia subyacente mediante el búfer y el desplazamiento especificados.Write the specified number of Bytes to the underlying stream using the specified buffer and offset.

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)

Parámetros

buffer
Byte[]

Matriz de Byte que proporciona los bytes escritos en la secuencia.A Byte array that supplies the bytes written to the stream.

offset
Int32

Int32 que contiene la ubicación de base cero de buffer donde empieza la lectura de los bytes que se van a escribir en la secuencia.A Int32 that contains the zero-based location in buffer at which to begin reading bytes to be written to the stream.

count
Int32

Int32 que contiene un número de bytes que se van a leer del buffer.A Int32 that contains the number of bytes to read from buffer.

Excepciones

buffer es null.buffer is null.

offset es menor que cero.offset is less than zero.

O bien-or-

offset es mayor que la longitud de buffer.offset is greater than the length of buffer.

O bien-or- La suma de offset y el recuento es mayor que la longitud de buffer.offset + count is greater than the length of buffer.

No se pudo realizar la operación de escritura.The write operation failed.

Ya hay una operación de escritura en curso.There is already a write operation in progress.

Este objeto se ha cerrado.This object has been closed.

No se ha producido la autenticación.Authentication has not occurred.

Comentarios

Este método se bloquea mientras se completa la operación.This method blocks while the operation completes. Para evitar el bloqueo mientras se completa la operación, use el BeginWrite método.To prevent blocking while the operation completes the operation completes, use the BeginWrite method.

No se puede llamar a este método hasta que se haya autenticado correctamente.You cannot call this method until you have successfully authenticated. Para autenticar, llame a uno de los AuthenticateAsClient métodos, o BeginAuthenticateAsClient , AuthenticateAsServer BeginAuthenticateAsServer .To authenticate call one of the AuthenticateAsClient, or BeginAuthenticateAsClient, AuthenticateAsServer, BeginAuthenticateAsServer methods.

La SslStream clase no admite varias operaciones de escritura simultáneas.The SslStream class does not support multiple simultaneous write operations.

Se aplica a