Export (0) Print
Expand All

NetworkStream.Write Method

Writes data to the NetworkStream.

[Visual Basic]
Overrides Public Sub Write( _
   ByVal buffer() As Byte, _
   ByVal offset As Integer, _
   ByVal size As Integer _
)
[C#]
public override void Write(
 byte[] buffer,
 int offset,
 int size
);
[C++]
public: void Write(
 unsigned char buffer __gc[],
 int offset,
 int size
);
[JScript]
public override function Write(
   buffer : Byte[],
 offset : int,
 size : int
);

Parameters

buffer
An array of type Byte that contains the data to write to the NetworkStream.
offset
The location in buffer from which to start writing data.
size
The number of bytes to write to the NetworkStream.

Exceptions

Exception Type Condition
ArgumentNullException buffer is a null reference (Nothing in Visual Basic).
ArgumentOutOfRangeException offset is less than 0.

-or-

offset is greater than the length of buffer.

-or-

size is less than 0.

-or-

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

IOException There is a failure while writing to the network.
ObjectDisposedException The NetworkStream is closed.

-or-

There is a failure reading from the network.

IOException An error occurred when accessing the socket. See the Remarks section for more information.

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 are sent or a SocketException is thrown. If you receive a SocketException, use SocketException.ErrorCode to obtain the specific error code. Once you have obtained this code, you can refer to the Windows Socket Version 2 API error code documentation in MSDN 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 a IOException check the InnerException property to determine if it was caused by a SocketException. If so, use ErrorCode to obtain the specific error code. Once you have obtained this code, you can refer to the Windows Socket Version 2 API error code documentation in MSDN for a detailed description of the error.

Example

[Visual Basic, C#, C++] The following example checks to see whether the NetworkStream is writable. If it is, then Write is used to write a small message.

[Visual Basic] 
' 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


[C#] 

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


[C++] 

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

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework, Common Language Infrastructure (CLI) Standard

See Also

NetworkStream Class | NetworkStream Members | System.Net.Sockets Namespace | CanWrite | Writeable

Show:
© 2014 Microsoft