NetworkStream.Write Method
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
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
- Source:
- NetworkStream.cs
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
The NetworkStream does not support writing.
An error occurred when accessing the socket.
-or-
There was a failure while writing to the network.
The NetworkStream is closed.
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.
Applies to
Write(Byte[], Int32, Int32)
- Source:
- NetworkStream.cs
- Source:
- NetworkStream.cs
- Source:
- NetworkStream.cs
- Source:
- NetworkStream.cs
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 count);
public:
override void Write(cli::array <System::Byte> ^ buffer, int offset, int size);
public override void Write(byte[] buffer, int offset, int count);
public override void Write(byte[] buffer, int offset, int size);
override this.Write : byte[] * int * int -> unit
override this.Write : byte[] * int * int -> unit
Public Overrides Sub Write (buffer As Byte(), offset As Integer, count As Integer)
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
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.
The NetworkStream is closed.
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)
{
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 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.
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.