System.Net.Sockets.Socket.SendToAsync Method

Sends data asynchronously to a specific remote host.

Syntax

public bool SendToAsync (SocketAsyncEventArgs e)

Parameters

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

Returns

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.

Remarks

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

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 Socket.SendToAsync(SocketAsyncEventArgs) 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 protocol, you must first call the Socket.Accept, Socket.AcceptAsync(SocketAsyncEventArgs), erload:System.Net.Sockets.Socket.BeginAccept, erload:System.Net.Sockets.Socket.BeginConnect, erload:System.Net.Sockets.Socket.Connect, or Socket.ConnectAsync(SocketAsyncEventArgs) method. Otherwise Socket.SendToAsync(SocketAsyncEventArgs) will throw a System.Net.Sockets.SocketException. When using a connection-oriented protocol, the Socket.SendToAsync(SocketAsyncEventArgs) method will ignore the SocketAsyncEventArgs.RemoteEndPoint property and send data to the System.Net.EndPoint established in the Socket.Accept, Socket.AcceptAsync(SocketAsyncEventArgs), erload:System.Net.Sockets.Socket.BeginAccept, erload:System.Net.Sockets.Socket.BeginConnect, erload:System.Net.Sockets.Socket.Connect, or Socket.ConnectAsync(SocketAsyncEventArgs) method.

If you are using a connectionless protocol, you do not need to establish a default remote host with the erload:System.Net.Sockets.Socket.BeginConnect, erload:System.Net.Sockets.Socket.Connect, or Socket.ConnectAsync(SocketAsyncEventArgs) method prior to calling Socket.SendToAsync(SocketAsyncEventArgs). You only need to do this if you intend to call the erload:System.Net.Sockets.Socket.BeginSend or Socket.SendAsync(SocketAsyncEventArgs) methods. If you do call the erload:System.Net.Sockets.Socket.BeginConnect, erload:System.Net.Sockets.Socket.Connect, or Socket.ConnectAsync(SocketAsyncEventArgs) method prior to calling Socket.SendToAsync(SocketAsyncEventArgs), the SocketAsyncEventArgs.RemoteEndPoint property will override the specified default remote host for that send operation only. You are also not required to call the Socket.Bind(System.Net.EndPoint) method. In this case, the underlying service provider will assign the most appropriate local network IP address and port number. Use a port number of zero if you want the underlying service provider to select a free port. If you need to identify the assigned local network IP address and port number, you can use the Socket.LocalEndPoint property after the SocketAsyncEventArgs.Completed event is signaled and the associated delegates are called.

If you want to send data to a broadcast address, you must first call the Socket.SetSocketOption(SocketOptionLevel, SocketOptionName, bool) method and set the socket option for SocketOptionName.Broadcast to true. 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.SendToAsync(SocketAsyncEventArgs) will throw a System.Net.Sockets.SocketException.

If you specify the DontRoute flag in the SocketAsyncEventArgs.SocketFlags property, the data you are sending will not be routed.

For message-oriented sockets, 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 Socket.SendToAsync(SocketAsyncEventArgs) will throw a System.Net.Sockets.SocketException. The successful completion of a Socket.SendToAsync(SocketAsyncEventArgs) method does not indicate that the data was successfully delivered.

Requirements

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