System.Net.Sockets.Socket.BeginSend Method

Sends data asynchronously to a connected System.Net.Sockets.Socket.

Syntax

public IAsyncResult BeginSend (byte[] buffer, int offset, int size, SocketFlags socket_flags, AsyncCallback callback, object state)

Parameters

socket_flags
Documentation for this section has not yet been entered.
buffer
An array of type byte that contains the data to send.
offset
The zero-based position in the buffer parameter at which to begin sending data.
size
The number of bytes to send.
callback
The AsyncCallback delegate.
state
An object that contains state information for this request.

Returns

An IAsyncResult that references the asynchronous send.

Exceptions

TypeReason
ArgumentNullException buffer is null.
ArgumentOutOfRangeException

offset < 0.

-or-

offset > buffer.Length.

-or-

size < 0.

-or-

size > buffer.Length - offset.

System.Net.Sockets.SocketException

socketFlags is not a valid combination of values.

-or-

An error occurred while accessing the socket.

Note: For additional information on causes of the SocketException, see the System.Net.Sockets.SocketException class.

ObjectDisposedExceptionThe current instance has been disposed.

Remarks

The Socket.BeginSend(Byte[], int, int, SocketFlags, AsyncCallback, object) method starts an asynchronous send operation to the remote host established in the Socket.Connect(System.Net.EndPoint), Socket.BeginConnect(System.Net.EndPoint, AsyncCallback, object), Socket.Accept, or Socket.BeginAccept(AsyncCallback, object) method. Socket.BeginSend(Byte[], int, int, SocketFlags, AsyncCallback, object) will throw an exception if you do not first call Socket.Accept, Socket.BeginAccept(AsyncCallback, object), Socket.Connect(System.Net.EndPoint), or Socket.BeginConnect(System.Net.EndPoint, AsyncCallback, object). Calling the Socket.BeginSend(Byte[], int, int, SocketFlags, AsyncCallback, object) method gives you the ability to send data within a separate execution thread.

You can create a callback method that implements the AsyncCallback delegate and pass its name to the Socket.BeginSend(Byte[], int, int, SocketFlags, AsyncCallback, object) method. To do this, at the very minimum, your state parameter must contain the connected or default System.Net.Sockets.Socket being used for communication. If your callback needs more information, you can create a small class or structure to hold the System.Net.Sockets.Socket and the other required information. Pass an instance of this class to the Socket.BeginSend(Byte[], int, int, SocketFlags, AsyncCallback, object) method through the state parameter.

Your callback method should invoke the Socket.EndSend(IAsyncResult) method. When your application calls Socket.BeginSend(Byte[], int, int, SocketFlags, AsyncCallback, object), the system will use a separate thread to execute the specified callback method, and will block on Socket.EndSend(IAsyncResult) until the System.Net.Sockets.Socket sends the number of bytes requested or throws an exception. If you want the original thread to block after you call the Socket.BeginSend(Byte[], int, int, SocketFlags, AsyncCallback, object) method, use the System.Threading.WaitHandle.WaitOne(int, bool) method. Call the Set method on a T:System.Threading.ManualResetEvent in the callback method when you want the original thread to continue executing. For additional information on writing callback methods see [<topic://cpconcallbacksample>].

Although intended for connection-oriented protocols, Socket.BeginSend(Byte[], int, int, SocketFlags, AsyncCallback, object) also works for connectionless protocols, provided that you first call the Socket.Connect(System.Net.EndPoint) or Socket.BeginConnect(System.Net.EndPoint, AsyncCallback, object) method to establish a default remote host. If you are using a connectionless protocol and plan to send data to several different hosts, you should use Socket.BeginSendTo(Byte[], int, int, SocketFlags, System.Net.EndPoint, AsyncCallback, object). It is okay to use Socket.BeginSendTo(Byte[], int, int, SocketFlags, System.Net.EndPoint, AsyncCallback, object) even after you have established a default remote host with Socket.Connect(System.Net.EndPoint). You can also change the default remote host prior to calling Socket.BeginSend(Byte[], int, int, SocketFlags, AsyncCallback, object) by making another call to Socket.Connect(System.Net.EndPoint) or Socket.BeginConnect(System.Net.EndPoint, AsyncCallback, object). With connectionless protocols, you must also be sure that the size of your buffer does not exceed the maximum packet size of the underlying service provider. If it does, the datagram will not be sent and Socket.BeginSend(Byte[], int, int, SocketFlags, AsyncCallback, object) will throw a System.Net.Sockets.SocketException.

If you specify the SocketFlags.DontRoute flag as the socketflags parameter, the data you are sending will not be routed.

Note:

If you receive a System.Net.Sockets.SocketException, use the SocketException.ErrorCode property to obtain the specific error code. After you have obtained this code, refer to the Windows Sockets version 2 API error code documentation in the MSDN library for a detailed description of the error.

Note:

All I/O initiated by a given thread is canceled when that thread exits. A pending asynchronous operation can fail if the thread exits before the operation completes.

Note:

state is an instantiation of a user-defined class.

Note:

The successful completion of a send does not indicate that the data was successfully delivered. If no buffer space is available within the transport system to hold the data to be transmitted, send will block unless the socket has been placed in nonblocking mode.

Note:

This member outputs trace information when you enable network tracing in your application. For more information, see [<topic://conUsingNetworkTracing>].

Note:

The execution context (the security context, the impersonated user, and the calling context) is cached for the asynchronous System.Net.Sockets.Socket methods. After the first use of a particular context (a specific asynchronous System.Net.Sockets.Socket method, a specific System.Net.Sockets.Socket instance, and a specific callback), subsequent uses of that context will see a performance improvement.

Example

For an outline of an asynchronous operation, see the Socket.BeginAccept(AsyncCallback, object) method. For the complete example, which uses the Socket.BeginSend(Byte[], int, int, SocketFlags, AsyncCallback, object) method, see the System.Net.Sockets.Socket class overview.

Requirements

Namespace: System.Net.Sockets
Assembly: System (in System.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0, 4.0.0.0