Export (0) Print
Expand All
1 out of 3 rated this helpful - Rate this topic

Socket.SendAsync Method

Sends data asynchronously to a connected Socket object.

Namespace:  System.Net.Sockets
Assembly:  System (in System.dll)
public bool SendAsync(
	SocketAsyncEventArgs e
)

Parameters

e
Type: System.Net.Sockets.SocketAsyncEventArgs

The System.Net.Sockets.SocketAsyncEventArgs object to use for this asynchronous socket operation.

Return Value

Type: System.Boolean
Returns true if the I/O operation is pending. The SocketAsyncEventArgs.Completed event on the e parameter will be raised upon completion of the operation.
Returns false if the I/O operation completed synchronously. In this case, The SocketAsyncEventArgs.Completed event on the e parameter will not be raised and the e object passed as a parameter may be examined immediately after the method call returns to retrieve the result of the operation.
ExceptionCondition
ArgumentException

The SocketAsyncEventArgs.Buffer or SocketAsyncEventArgs.BufferList properties on the e parameter must reference valid buffers. One or the other of these properties may be set, but not both at the same time.

InvalidOperationException

A socket operation was already in progress using the System.Net.Sockets.SocketAsyncEventArgs object specified in the e parameter.

NotSupportedException

Windows XP or later is required for this method.

ObjectDisposedException

The Socket has been closed.

SocketException

The Socket is not yet connected or was not obtained via an Accept, AcceptAsync,or BeginAccept, method.

The SendAsync method is used to write outgoing data from one or more buffers on a connection-oriented socket. This method can also be used, however, on connectionless sockets that have specified a remote host on a connect operation.

The SendAsync method starts an asynchronous send operation to the remote host established in the Accept, AcceptAsync, BeginAccept, BeginConnect, Connect, or ConnectAsync method.

The following properties and events on the System.Net.Sockets.SocketAsyncEventArgs object are required to successfully call this method:

The caller may set the SocketAsyncEventArgs.UserToken property to any user state object desired before calling the SendAsync method, so that the information will be retrievable in the callback method. If the callback needs more information than a single object, a small class can be created to hold the other required state information as members.

The SendAsync method will throw an exception if you do not first call Accept, AcceptAsync, BeginAccept BeginConnect, Connect, or ConnectAsync.

Calling the SendAsync method gives you the ability to send data within a separate execution thread.

For message-oriented sockets, do not exceed the maximum message size of the underlying Windows sockets service provider. If the data is too long to pass atomically through the underlying service provider, no data is transmitted and the SendAsync method throws a SocketException with the SocketAsyncEventArgs.SocketError set to the native Winsock WSAEMSGSIZE error code (10040).

Note that the successful completion of the SendAsync method does not indicate that the data was successfully delivered.

Windows 7, Windows Vista, Windows XP SP2, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5 SP1, 3.0 SP1, 2.0 SP1
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.