Export (0) Print
Expand All

NetworkStream.Write Method

Writes data to the NetworkStream.

Namespace: System.Net.Sockets
Assembly: System (in system.dll)

public override void Write (
	byte[] buffer,
	int offset,
	int size
)
public void Write (
	byte[] buffer, 
	int offset, 
	int size
)
public override function Write (
	buffer : byte[], 
	offset : int, 
	size : int
)
Not applicable.

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.

Exception typeCondition

ArgumentNullException

The buffer parameter is a null reference (Nothing in Visual Basic).

ArgumentOutOfRangeException

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.

IOException

There was a failure while writing to the network.

-or-

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

ObjectDisposedException

The NetworkStream is closed.

-or-

There was a failure reading from the network.

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 in MSDN for a detailed description of the error.

NoteNote:

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.

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

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


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

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft