Write Method

NegotiateStream.Write Method

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


Type: System.Byte[]
A Byte array that supplies the bytes written to the stream.
Type: System.Int32
An Int32 containing the zero-based location in buffer at which to begin reading bytes to be written to the stream.
Type: System.Int32
A Int32 containing the number of bytes to read from buffer.


buffer is null.


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.


The write operation failed.

- or -

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


There is already a write operation in progress.


This object has been closed.


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 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.
        Console.WriteLine("Client connected to {0}.",
        // 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.
             (NetworkCredential) CredentialCache.DefaultCredentials, 
        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);
            Console.WriteLine("Sent {0} bytes.", message.Length);
     // Close the client connection.
        Console.WriteLine("Client closed.");


.NET Framework

Supported in: 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

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

Community Additions

© 2015 Microsoft