NetworkStream.Write NetworkStream.Write NetworkStream.Write NetworkStream.Write Method

Definition

Overloads

Write(ReadOnlySpan<Byte>) Write(ReadOnlySpan<Byte>) Write(ReadOnlySpan<Byte>) Write(ReadOnlySpan<Byte>)

Writes data to the NetworkStream from a read-only byte span.

Write(Byte[], Int32, Int32) Write(Byte[], Int32, Int32) Write(Byte[], Int32, Int32) Write(Byte[], Int32, Int32)

Writes data to the NetworkStream from a specified range of a byte array.

Write(ReadOnlySpan<Byte>) Write(ReadOnlySpan<Byte>) Write(ReadOnlySpan<Byte>) Write(ReadOnlySpan<Byte>)

Writes data to the NetworkStream from a read-only byte span.

public:
 override void Write(ReadOnlySpan<System::Byte> buffer);
public override void Write (ReadOnlySpan<byte> buffer);
override this.Write : ReadOnlySpan<byte> -> unit
Public Overrides Sub Write (buffer As ReadOnlySpan(Of Byte))

Parameters

buffer
ReadOnlySpan<Byte>

The data to write to the NetworkStream.

Exceptions

An error occurred when accessing the socket.

-or-

There was a failure while writing to the network.

Remarks

This method sends all bytes in buffer to the network. The Write method blocks until the requested number of bytes is sent or a SocketException is thrown.

Note

Check to see if the NetworkStream is writable by calling the CanWrite property. If you attempt to write to a NetworkStream that is not writable, you will get an InvalidOperationException.

Note

If you receive an IOException, check the InnerException property to determine if it was caused by a SocketException. If so, use the 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.

Write(Byte[], Int32, Int32) Write(Byte[], Int32, Int32) Write(Byte[], Int32, Int32) Write(Byte[], Int32, Int32)

Writes data to the NetworkStream from a specified range of a byte array.

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.

An error occurred when accessing the socket.

-or-

There was a failure while writing to 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

This 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.

Note

Check to see if the NetworkStream is writable by calling the CanWrite property. If you attempt to write to a NetworkStream that is not writable, you will get an InvalidOperationException.

Note

If you receive an IOException, check the InnerException property to determine if it was caused by a SocketException. If so, use the 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.

See also

Applies to