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

Definizione

Scrivere il numero specificato di Bytes nel flusso sottostante usando il buffer e l'offset specificati.

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)

Parametri

buffer
Byte[]

Matrice Byte che fornisce i byte scritti nel flusso.

offset
Int32

Oggetto Int32 contenente la posizione in base zero in in buffer cui iniziare la lettura dei byte da scrivere nel flusso.

count
Int32

Oggetto Int32 contenente il numero di byte da leggere da buffer.

Eccezioni

buffer è null.

offset è minore di 0.

oppure

offset è maggiore della lunghezza di buffer.

oppure

offset più conteggio è maggiore della lunghezza di buffer.

Operazione di scrittura non riuscita.

oppure

La crittografia è in uso, ma i dati non possono essere crittografati.

È già in corso un'operazione di scrittura.

Questo oggetto è stato chiuso.

L'autenticazione non è stata eseguita.

Esempio

Nell'esempio di codice seguente viene illustrato come scrivere in un oggetto NegotiateStream.

    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.");
}

Commenti

Se la crittografia, la firma o la crittografia e la firma sono abilitate, questo metodo legge i dati dal buffer, crittografa, firma o li firma e li trasmette usando il flusso sottostante. Se non sono in uso servizi di sicurezza come la crittografia dei dati o la firma, questo metodo richiama Write nel flusso sottostante.

Questo metodo si blocca mentre l'operazione di scrittura viene completata. Per impedire il blocco al termine dell'operazione, utilizzare il WriteAsync metodo .

Non è possibile chiamare questo metodo fino a quando non è stata eseguita correttamente l'autenticazione. Per eseguire l'autenticazione, chiamare uno dei AuthenticateAsClientmetodi , AuthenticateAsClientAsyncBeginAuthenticateAsClient, AuthenticateAsServer, AuthenticateAsServerAsync, o BeginAuthenticateAsServer .

La NegotiateStream classe non supporta più operazioni di scrittura simultanee. Se si tenta di avviare un'operazione di scrittura mentre un'altra operazione di scrittura è già in esecuzione nello stesso flusso, verrà generata un'eccezione NotSupportedException .

Si applica a