Share via


NegotiateStream.Write(Byte[], Int32, Int32) Méthode

Définition

Écrivez le nombre spécifié de Bytes dans le flux sous-jacent à l'aide de la mémoire tampon et de l'offset spécifié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)

Paramètres

buffer
Byte[]

Tableau de Byte qui fournit les octets écrits dans le flux.

offset
Int32

Int32 contenant l’emplacement de base zéro dans buffer à partir duquel commencer à lire les octets devant être écrits dans le flux de données.

count
Int32

Int32 contenant le nombre d'octets à lire dans buffer.

Exceptions

buffer a la valeur null.

offset is less than 0.

- ou -

offset est supérieur à la longueur de buffer.

- ou -

offset plus count est supérieur à la longueur de buffer.

L'opération d'écriture a échoué.

- ou -

Le chiffrement est utilisé, mais les données n'ont pas pu être chiffrées.

Une opération d'écriture est déjà en cours.

L’objet a été fermé.

L'authentification n'a pas été effectuée.

Exemples

L’exemple de code suivant illustre l’écriture dans un NegotiateStream.

int main()
{
   
   // Establish the remote endpoint for the socket.
   // For this example, use the local machine.
   IPHostEntry^ ipHostInfo = Dns::GetHostEntry( Dns::GetHostName() );
   IPAddress^ ipAddress = ipHostInfo->AddressList[ 0 ];
   
   // Client and server use port 11000. 
   IPEndPoint^ remoteEP = gcnew IPEndPoint( ipAddress,11000 );
   
   // Create a TCP/IP socket.
   TcpClient^ client = gcnew TcpClient;
   
   // Connect the socket to the remote endpoint.
   client->Connect( remoteEP );
   Console::WriteLine( L"Client connected to {0}.", remoteEP );
   
   // Ensure the client does not close when there is 
   // still data to be sent to the server.
   client->LingerState = (gcnew LingerOption( true,0 ));
   
   // Request authentication.
   NetworkStream^ clientStream = client->GetStream();
   NegotiateStream^ authStream = gcnew NegotiateStream( clientStream );
   
   // Request authentication for the client only (no mutual authentication).
   // Authenicate using the client's default credetials.
   // Permit the server to impersonate the client to access resources on the server only.
   // Request that data be transmitted using encryption and data signing.
   authStream->AuthenticateAsClient( dynamic_cast<NetworkCredential^>(CredentialCache::DefaultCredentials), 
          L"", 
          ProtectionLevel::EncryptAndSign, 
          TokenImpersonationLevel::Impersonation );
   
   DisplayAuthenticationProperties( authStream );
   DisplayStreamProperties( authStream );
   if ( authStream->CanWrite )
   {
      
      // Encode the test data into a byte array.
      array<Byte>^message = System::Text::Encoding::UTF8->GetBytes( L"Hello from the client." );
      authStream->Write( message, 0, message->Length );
      authStream->Flush();
      Console::WriteLine( L"Sent {0} bytes.", message->Length );
   }

   
   // Close the client connection.
   authStream->Close();
   Console::WriteLine( L"Client closed." );
}

    public static void Main(String[] args)
    {
        // Establish the remote endpoint for the socket.
        // For this example, use the local machine.
        IPHostEntry ipHostInfo = Dns.GetHostEntry(Dns.GetHostName());
        IPAddress ipAddress = ipHostInfo.AddressList[0];
        // Client and server use port 11000.
        IPEndPoint remoteEP = new IPEndPoint(ipAddress,11000);
        // Create a TCP/IP socket.
       TcpClient client = new TcpClient();
        // Connect the socket to the remote endpoint.
        client.Connect(remoteEP);
        Console.WriteLine("Client connected to {0}.",
            remoteEP.ToString());
        // Ensure the client does not close when there is
        // still data to be sent to the server.
        client.LingerState = (new LingerOption(true,0));
        // Request authentication.
        NetworkStream clientStream = client.GetStream();
        NegotiateStream authStream = new NegotiateStream(clientStream);
        // Request authentication for the client only (no mutual authentication).
        // Authenicate using the client's default credetials.
        // Permit the server to impersonate the client to access resources on the server only.
        // Request that data be transmitted using encryption and data signing.
        authStream.AuthenticateAsClient(
             (NetworkCredential) CredentialCache.DefaultCredentials,
             "",
             ProtectionLevel.EncryptAndSign,
             TokenImpersonationLevel.Impersonation);
        DisplayAuthenticationProperties(authStream);
        DisplayStreamProperties(authStream);
        if (authStream.CanWrite)
        {
             // Encode the test data into a byte array.
            byte[] message = System.Text.Encoding.UTF8.GetBytes("Hello from the client.");
            authStream.Write(message, 0, message.Length);
            authStream.Flush();
            Console.WriteLine("Sent {0} bytes.", message.Length);
        }
        // Close the client connection.
        authStream.Close();
        Console.WriteLine("Client closed.");
}

Remarques

Si le chiffrement, la signature ou le chiffrement et la signature sont activés, cette méthode lit les données de la mémoire tampon, chiffre, signe ou chiffre et signe, et les transmet à l’aide du flux sous-jacent. Si aucun service de sécurité tel que le chiffrement ou la signature des données n’est en cours d’utilisation, cette méthode appelle Write sur le flux sous-jacent.

Cette méthode est bloquée lorsque l’opération d’écriture se termine. Pour éviter le blocage à la fin de l’opération, utilisez la WriteAsync méthode .

Vous ne pouvez pas appeler cette méthode tant que vous n’avez pas réussi à vous authentifier. Pour vous authentifier, appelez l’une AuthenticateAsClientdes méthodes , AuthenticateAsClientAsync, BeginAuthenticateAsClient, AuthenticateAsServerAuthenticateAsServerAsync, ou BeginAuthenticateAsServer .

La NegotiateStream classe ne prend pas en charge plusieurs opérations d’écriture simultanées. Si vous tentez de démarrer une opération d’écriture alors qu’une autre opération d’écriture s’exécute déjà sur le même flux, une NotSupportedException exception est levée.

S’applique à