Bagikan melalui


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

Definisi

Tulis jumlah Byteyang ditentukan ke aliran yang mendasar menggunakan buffer dan offset yang ditentukan.

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)

Parameter

buffer
Byte[]

Array Byte yang memasok byte yang ditulis ke aliran.

offset
Int32

Yang Int32 berisi lokasi berbasis nol untuk buffer mulai membaca byte yang akan ditulis ke aliran.

count
Int32

yang Int32 berisi jumlah byte untuk dibaca dari buffer.

Pengecualian

bufferadalah null.

offset is less than 0.

-atau-

offset lebih besar dari panjang buffer.

-atau-

offset jumlah plus lebih besar dari panjang buffer.

Operasi tulis gagal.

-atau-

Enkripsi sedang digunakan, tetapi data tidak dapat dienkripsi.

Sudah ada operasi tulis yang sedang berlangsung.

Objek ini telah ditutup.

Autentikasi belum terjadi.

Contoh

Contoh kode berikut menunjukkan penulisan ke 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.");
}

Keterangan

Jika enkripsi, penandatanganan, atau enkripsi dan penandatanganan diaktifkan, metode ini membaca data dari buffer, mengenkripsi, menandatangani, atau mengenkripsi dan menandatanganinya, dan mengirimkannya menggunakan aliran yang mendasar. Jika tidak ada layanan keamanan seperti enkripsi data atau penandatanganan yang digunakan, metode ini memanggil pada aliran yang mendasar Write .

Metode ini memblokir saat operasi tulis selesai. Untuk mencegah pemblokiran saat operasi selesai, gunakan WriteAsync metode .

Anda tidak dapat memanggil metode ini sampai Anda berhasil mengautentikasi. Untuk mengautentikasi, panggil salah AuthenticateAsClientsatu metode , , AuthenticateAsClientAsyncBeginAuthenticateAsClient, AuthenticateAsServer, AuthenticateAsServerAsyncatau BeginAuthenticateAsServer .

Kelas NegotiateStream tidak mendukung beberapa operasi tulis simultan. Jika Anda mencoba memulai operasi tulis saat operasi tulis lain sudah dijalankan pada aliran yang sama, NotSupportedException pengecualian akan dilemparkan.

Berlaku untuk