NetworkStream.BeginWrite(Byte[], Int32, Int32, AsyncCallback, Object) NetworkStream.BeginWrite(Byte[], Int32, Int32, AsyncCallback, Object) NetworkStream.BeginWrite(Byte[], Int32, Int32, AsyncCallback, Object) NetworkStream.BeginWrite(Byte[], Int32, Int32, AsyncCallback, Object) Method

Definition

Begins an asynchronous write to a stream.

public:
 override IAsyncResult ^ BeginWrite(cli::array <System::Byte> ^ buffer, int offset, int size, AsyncCallback ^ callback, System::Object ^ state);
public override IAsyncResult BeginWrite (byte[] buffer, int offset, int size, AsyncCallback callback, object state);
override this.BeginWrite : byte[] * int * int * AsyncCallback * obj -> IAsyncResult
Public Overrides Function BeginWrite (buffer As Byte(), offset As Integer, size As Integer, callback As AsyncCallback, state As Object) As IAsyncResult
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 to begin sending the data.

size
Int32 Int32 Int32 Int32

The number of bytes to write to the NetworkStream.

state
Object Object Object Object

An object that contains any additional user-defined data.

Returns

An IAsyncResult that represents the asynchronous call.

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.

The underlying Socket is closed.

-or-

There was a failure while writing to the network.

-or-

An error occurred when accessing the socket.

Examples

The following code example uses BeginWrite to write data asynchronously to a network stream. The myWriteCallBack method implements the AsyncCallback delegate and is called by the system when BeginWrite returns.

// Example of CanWrite, and BeginWrite.
// Check to see if this NetworkStream is writable.
if ( myNetworkStream->CanWrite )
{
   array<Byte>^myWriteBuffer = Encoding::ASCII->GetBytes( "Are you receiving this message?" );
   myNetworkStream->BeginWrite( myWriteBuffer, 0, myWriteBuffer->Length, gcnew AsyncCallback( &MyNetworkStreamClass::myWriteCallBack ), myNetworkStream );
   allDone->WaitOne();
}
else
{
   Console::WriteLine( "Sorry.  You cannot write to this NetworkStream." );
}
//Example of CanWrite, and BeginWrite.

 // Check to see if this NetworkStream is writable.
 if (myNetworkStream.CanWrite){
    
      byte[] myWriteBuffer = Encoding.ASCII.GetBytes("Are you receiving this message?");
      myNetworkStream.BeginWrite(myWriteBuffer, 0, myWriteBuffer.Length, 
                                                   new AsyncCallback(NetworkStream_ASync_Send_Receive.myWriteCallBack), 
                                                   myNetworkStream);
      allDone.WaitOne();
 }
 else{
      Console.WriteLine("Sorry.  You cannot write to this NetworkStream.");  
 }

' Example of CanWrite, and BeginWrite.
' 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.BeginWrite(myWriteBuffer, 0, myWriteBuffer.Length, New AsyncCallback(AddressOf NetworkStream_ASync_Send_Receive.myWriteCallBack), myNetworkStream)
   allDone.WaitOne()
Else
   Console.WriteLine("Sorry.  You cannot write to this NetworkStream.")
End If

Remarks

The BeginWrite method starts an asynchronous send operation to the remote host. Calling the BeginWrite method gives you the ability to send data within a separate execution thread.

You must create a callback method that implements the AsyncCallback delegate and pass its name to the BeginWrite method. At the very minimum, your state parameter must contain the NetworkStream. If your callback needs more information, you can create a small class or structure to hold the NetworkStream and the other required information. Pass the structure or class instance to the BeginWrite method through the state parameter.

Your callback method should implement the EndWrite method. When your application calls BeginWrite, the system uses a separate thread to execute the specified callback method, and blocks on EndWrite until the NetworkStream sends the number of bytes requested or throws an exception. If you want the original thread to block after you call the BeginWrite method, use the WaitOne method. Call Set in the callback method when you want the original thread to continue executing. For additional information about writing callback methods, see Marshaling a Delegate as a Callback Method.

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.

Read and write operations can be performed simultaneously on an instance of the NetworkStream class without the need for synchronization. As long as there is one unique thread for the write operations and one unique thread for the read operations, there will be no cross-interference between read and write threads and no synchronization is required.

Applies to

See Also