Partager via


Socket.SendToAsync Method

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Sends data asynchronously to a specific remote host.

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

Syntax

'Declaration
Public Function SendToAsync ( _
    e As SocketAsyncEventArgs _
) As Boolean
public bool SendToAsync(
    SocketAsyncEventArgs e
)

Parameters

Return Value

Type: System.Boolean
Returns true if the I/O operation is pending. The 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 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.

Exceptions

Exception Condition
ArgumentNullException

The SocketAsyncEventArgs.RemoteEndPoint cannot be null.

InvalidOperationException

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

ObjectDisposedException

The Socket has been closed.

SocketException

An error occurred when attempting to access the socket. This error can occur if socket specified is connection-oriented of type Stream , but the Socket is not yet connected.

Remarks

The SendToAsync(SocketAsyncEventArgs) method starts an asynchronous send operation to the remote host specified in the RemoteEndPoint property of the e parameter. Calling the SendToAsync(SocketAsyncEventArgs) method gives you the ability to send data within a separate execution thread. Although this method is intended for connectionless sockets of type Dgram, the SendToAsync(SocketAsyncEventArgs) method also works with connection-oriented sockets of type Stream.

To be notified of completion, you must create a callback method that implements the EventHandler<SocketAsyncEventArgs> delegate and attach the callback to the SocketAsyncEventArgs.Completed event.

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 SendToAsync 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.

If you are using a connection-oriented socket of type Stream, you must first call the ConnectAsync method. Otherwise SendToAsync will throw a SocketException. When using a connection-oriented socket of type Stream, the SendToAsync method will ignore the SocketAsyncEventArgs.RemoteEndPoint property and send data to the System.Net.EndPoint established in the ConnectAsync method.

If you are using a connectionless socket of type Dgram, you do not need to establish a default remote host with the ConnectAsync method prior to calling SendToAsync. You only need to do this if you intend to call the SendAsync method. If you do call the ConnectAsync method prior to calling SendToAsync, the SocketAsyncEventArgs.RemoteEndPoint property will override the specified default remote host for that send operation only.

For a message-oriented socket of type Dgram, care must be taken not to exceed the maximum message size of the underlying transport. If the size of the buffer exceeds the maximum packet size of the underlying service provider, the datagram is not sent and SendToAsync will throw a SocketException. The successful completion of a SendToAsync method does not indicate that the data was successfully delivered to the remote host. A successful completion only indicates that the data was successfully sent on the network from the local computer.

NoteNote:

If you receive a SocketException when calling this method, use the SocketException.ErrorCode property to obtain the specific error code. After you have obtained this code, refer to the Windows Sockets Error Codes documentation in the MSDN library for a detailed description of the error.

Version Notes

Silverlight for the desktop

Do not use for a Silverlight for the desktop application.

Version Information

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.