NetworkStream.Write Method

Definition

Overloads

Write(ReadOnlySpan<Byte>)

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

Write(Byte[], Int32, Int32)

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

Write(ReadOnlySpan<Byte>)

Source:
NetworkStream.cs
Source:
NetworkStream.cs
Source:
NetworkStream.cs

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

C#
public override void Write(ReadOnlySpan<byte> buffer);

Parameters

buffer
ReadOnlySpan<Byte>

The data to write to the NetworkStream.

Exceptions

The NetworkStream does not support writing.

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.

Poznámka

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.

Poznámka

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.

Applies to

.NET 10 a ďalšie verzie
Produkt Verzie
.NET Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Standard 2.1

Write(Byte[], Int32, Int32)

Source:
NetworkStream.cs
Source:
NetworkStream.cs
Source:
NetworkStream.cs

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

C#
public override void Write(byte[] buffer, int offset, int count);
C#
public override void Write(byte[] buffer, int offset, int size);

Parameters

buffer
Byte[]

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

offset
Int32

The location in buffer from which to start writing data.

countsize
Int32

The number of bytes to write to the NetworkStream.

Exceptions

The buffer parameter is null.

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.

The NetworkStream does not support writing.

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.

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

Remarks

This method starts at the specified offset and sends count 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.

Poznámka

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.

Poznámka

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

.NET 10 a ďalšie verzie
Produkt Verzie
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.3, 1.4, 1.6, 2.0, 2.1