NegotiateStream.Write(Byte[], Int32, Int32) Metoda

Definicja

Zapisz określoną liczbę Bytes do bazowego strumienia przy użyciu określonego buforu i przesunięcia.

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)

Parametry

buffer
Byte[]

Tablica dostarczająca Byte bajty zapisywane w strumieniu.

offset
Int32

Obiekt Int32 zawierający lokalizację opartą na zerach, w buffer której należy rozpocząć odczytywanie bajtów do zapisu w strumieniu.

count
Int32

Element Int32 zawierający liczbę bajtów do odczytania z bufferelementu .

Wyjątki

buffer to null.

offset is less than 0.

-lub-

offsetjest większa niż długość .buffer

-lub-

offset plus count jest większa niż długość buffer.

Operacja zapisu nie powiodła się.

-lub-

Szyfrowanie jest używane, ale nie można zaszyfrować danych.

Trwa już operacja zapisu.

Ten obiekt został zamknięty.

Uwierzytelnianie nie wystąpiło.

Przykłady

W poniższym przykładzie kodu pokazano zapisywanie w obiekcie 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.");
}

Uwagi

Jeśli włączono szyfrowanie, podpisywanie lub szyfrowanie i podpisywanie, ta metoda odczytuje dane z buforu, szyfruje, podpisuje lub szyfruje je i podpisuje oraz przesyła je przy użyciu bazowego strumienia. Jeśli nie są używane żadne usługi zabezpieczeń, takie jak szyfrowanie danych lub logowanie, ta metoda wywołuje Write na bazowym strumieniu.

Ta metoda blokuje działanie podczas wykonywania operacji zapisu. Aby zapobiec blokowaniu podczas wykonywania operacji, użyj WriteAsync metody .

Nie można wywołać tej metody do momentu pomyślnego uwierzytelnienia. Aby przeprowadzić uwierzytelnianie, wywołaj AuthenticateAsClientjedną z metod , , AuthenticateAsClientAsyncBeginAuthenticateAsClient, AuthenticateAsServer, AuthenticateAsServerAsynclub BeginAuthenticateAsServer .

Klasa NegotiateStream nie obsługuje wielu jednoczesnych operacji zapisu. Jeśli spróbujesz uruchomić operację zapisu, gdy inna operacja zapisu jest już wykonywana na tym samym strumieniu, NotSupportedException zostanie zgłoszony wyjątek.

Dotyczy