- socket_flags
- Documentation for this section has not yet been entered.
- remote_end
- 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 buffer 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.
An IAsyncResult that references the asynchronous send.
Type Reason ArgumentNullException buffer is null.
-or-
remoteEP 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.ObjectDisposedException The current instance has been disposed. System.Security.SecurityException A caller in the call stack does not have the required permissions.
The Socket.BeginSendTo(Byte[], int, int, SocketFlags, System.Net.EndPoint, AsyncCallback, object) method starts an asynchronous send operation to the remote host specified in the remoteEP parameter. Calling the Socket.BeginSendTo(Byte[], int, int, SocketFlags, System.Net.EndPoint, AsyncCallback, object) method gives you the ability to send data within a separate execution thread. Although intended for connectionless protocols, Socket.BeginSendTo(Byte[], int, int, SocketFlags, System.Net.EndPoint, AsyncCallback, object) works with both connectionless and connection-oriented protocols.
You can create a callback method that implements the AsyncCallback delegate and pass its name to the Socket.BeginSendTo(Byte[], int, int, SocketFlags, System.Net.EndPoint, 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 to hold the System.Net.Sockets.Socket, and the other required information. Pass an instance of this class to the Socket.BeginSendTo(Byte[], int, int, SocketFlags, System.Net.EndPoint, AsyncCallback, object) method through the state parameter.
Your callback method should invoke the Socket.EndSendTo(IAsyncResult) method. When your application calls Socket.BeginSendTo(Byte[], int, int, SocketFlags, System.Net.EndPoint, AsyncCallback, object), the system will use a separate thread to execute the specified callback method, and will block on Socket.EndSendTo(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.BeginSendTo(Byte[], int, int, SocketFlags, System.Net.EndPoint, 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 about writing callback methods see [<topic://cpconcallbacksample>].
If you are using a connection-oriented protocol, you must first call the Socket.Connect(System.Net.EndPoint), Socket.BeginConnect(System.Net.EndPoint, AsyncCallback, object), Socket.Accept, or Socket.BeginAccept(AsyncCallback, object) method, or Socket.BeginSendTo(Byte[], int, int, SocketFlags, System.Net.EndPoint, AsyncCallback, object) will throw a System.Net.Sockets.SocketException. Socket.BeginSendTo(Byte[], int, int, SocketFlags, System.Net.EndPoint, AsyncCallback, object) will ignore the remoteEP parameter and send data to the System.Net.EndPoint established in the Socket.Connect(System.Net.EndPoint), Socket.BeginConnect(System.Net.EndPoint, AsyncCallback, object), Socket.Accept, or Socket.BeginAccept(AsyncCallback, object) method.
If you are using a connectionless protocol, you do not need to establish a default remote host with the Socket.Connect(System.Net.EndPoint) or Socket.BeginConnect(System.Net.EndPoint, AsyncCallback, object) method prior to calling Socket.SendTo(Byte[], int, int, SocketFlags, System.Net.EndPoint). You only need to do this if you intend to call the Socket.BeginSend(Byte[], int, int, SocketFlags, AsyncCallback, object) method. If you do call the Socket.Connect(System.Net.EndPoint) or Socket.BeginConnect(System.Net.EndPoint, AsyncCallback, object) method prior to calling Socket.SendTo(Byte[], int, int, SocketFlags, System.Net.EndPoint), the remoteEP parameter 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 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 address and port number, you can use the Socket.LocalEndPoint property after the Socket.EndSendTo(IAsyncResult) method successfully completes.
If you want to send data to a broadcast address, you must first call the Socket.SetSocketOption(SocketOptionLevel, SocketOptionName, int) method and set the socket option to SocketOptionName.Broadcast. -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.EndSendTo(IAsyncResult) 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.
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.
This member outputs trace information when you enable network tracing in your application. For more information, see [<topic://conUsingNetworkTracing>].
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.
Type | Reason |
---|---|
System.Net.SocketPermission | Requires permission to make a connection to the endpoint defined by remoteEP. See System.Net.NetworkAccess.Connect. |