NetworkStream.Write(Byte[], Int32, Int32) NetworkStream.Write(Byte[], Int32, Int32) NetworkStream.Write(Byte[], Int32, Int32) NetworkStream.Write(Byte[], Int32, Int32) Method

Definition

Writes data to the NetworkStream.

public:
 override void Write(cli::array <System::Byte> ^ buffer, int offset, int size);
public override void Write (byte[] buffer, int offset, int size);
override this.Write : byte[] * int * int -> unit
Public Overrides Sub Write (buffer As Byte(), offset As Integer, size As Integer)
Parameters
buffer
Byte[]

An array of type Byte that contains the data to write to the NetworkStream.

offset
Int32 Int32 Int32 Int32

The location in buffer from which to start writing data.

size
Int32 Int32 Int32 Int32

The number of bytes to write to the NetworkStream.

Exceptions

The offset parameter is less than 0.

-or-

The offset parameter is greater than the length of buffer.

-or-

The size parameter is less than 0.

-or-

The size parameter is greater than the length of buffer minus the value of the offset parameter.

There was a failure while writing to the network.

-or-

An error occurred when accessing the socket.

The NetworkStream is closed.

-or-

There was a failure reading from the network.

Examples

The following code example checks to see whether the NetworkStream is writable. If it is, then Write is used to write a small message.

// Examples for CanWrite, and CanWrite  
// Check to see if this NetworkStream is writable.
if ( myNetworkStream->CanWrite )
{
   array<Byte>^ myWriteBuffer = Encoding::ASCII->GetBytes(
      "Are you receiving this message?" );
   myNetworkStream->Write( myWriteBuffer, 0, myWriteBuffer->Length );
}
else
{
   Console::WriteLine( "Sorry.  You cannot write to this NetworkStream." );
}
// Examples for CanWrite, and CanWrite  

// Check to see if this NetworkStream is writable.
if (myNetworkStream.CanWrite){
   
     byte[] myWriteBuffer = Encoding.ASCII.GetBytes("Are you receiving this message?");
     myNetworkStream.Write(myWriteBuffer, 0, myWriteBuffer.Length);
}
else{
     Console.WriteLine("Sorry.  You cannot write to this NetworkStream.");  
}

' Examples for CanWrite, and CanWrite  
' Check to see if this NetworkStream is writable.
If myNetworkStream.CanWrite Then
   
   Dim myWriteBuffer As Byte() = Encoding.ASCII.GetBytes("Are you receiving this message?")
   myNetworkStream.Write(myWriteBuffer, 0, myWriteBuffer.Length)
Else
   Console.WriteLine("Sorry.  You cannot write to this NetworkStream.")
End If

Remarks

The Write method starts at the specified offset and sends size bytes from the contents of buffer to the network. The Write method blocks until the requested number of bytes is sent or a SocketException is thrown. If you receive a SocketException, use the SocketException.ErrorCode property to obtain the specific error code and refer to the Windows Sockets version 2 API error code documentation for a detailed description of the error.

Note

Check to see if the NetworkStream is writable by accessing the CanWrite property. If you attempt to write to a NetworkStream that is not writable, you will get an IOException. If you receive an IOException, check the InnerException property to determine if it was caused by a SocketException.

Applies to

See Also