Export (0) Print
Expand All

NegotiateStream.Write Method

Note: This method is new in the .NET Framework version 2.0.

Write the specified number of Bytes to the underlying stream using the specified buffer and offset.

Namespace: System.Net.Security
Assembly: System (in system.dll)

public override void Write (
	byte[] buffer,
	int offset,
	int count
)
public void Write (
	byte[] buffer, 
	int offset, 
	int count
)
public override function Write (
	buffer : byte[], 
	offset : int, 
	count : int
)

Parameters

buffer

A Byte array that supplies the bytes written to the stream.

offset

An Int32 containing the zero-based location in buffer at which to begin reading bytes to be written to the stream.

count

A Int32 containing the number of bytes to read from buffer.

Exception typeCondition

ArgumentNullException

buffer is a null reference (Nothing in Visual Basic).

ArgumentException

offset is less than 0.

- or -

offset is greater than the length of buffer.

- or -

offset plus count is greater than the length of buffer.

IOException

The write operation failed.

- or -

Encryption is in use, but the data could not be encrypted.

NotSupportedException

There is already a write operation in progress.

ObjectDisposedException

This object has been closed.

InvalidOperationException

Authentication has not occurred.

If encryption, signing, or encryption and signing are enabled, this method reads the data from buffer, encrypts, signs, or encrypts and signs it, and transmits it using the underlying stream. If no security services such as data encryption or signing are in use, this method invokes M:Write on the underlying stream.

This method blocks while the write operation completes. To prevent blocking while the operation completes, use the Write method.

You cannot call this method until you have successfully authenticated. To authenticate, call one of the AuthenticateAsClient, BeginAuthenticateAsClient, AuthenticateAsServer, or BeginAuthenticateAsServer methods.

The NegotiateStream class does not support multiple simultaneous write operations. If you attempt to start a write operation while another write operation is already executing on the same stream, a NotSupportedException exception will be thrown.

The following code example demonstrates writing to a 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.");
        
}

Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0

Community Additions

ADD
Show:
© 2014 Microsoft