Upraviť

Zdieľať cez


Socket.BeginSendTo Method

Definition

Sends data asynchronously to a specific remote host.

public:
 IAsyncResult ^ BeginSendTo(cli::array <System::Byte> ^ buffer, int offset, int size, System::Net::Sockets::SocketFlags socketFlags, System::Net::EndPoint ^ remoteEP, AsyncCallback ^ callback, System::Object ^ state);
public IAsyncResult BeginSendTo (byte[] buffer, int offset, int size, System.Net.Sockets.SocketFlags socketFlags, System.Net.EndPoint remoteEP, AsyncCallback? callback, object? state);
public IAsyncResult BeginSendTo (byte[] buffer, int offset, int size, System.Net.Sockets.SocketFlags socketFlags, System.Net.EndPoint remoteEP, AsyncCallback callback, object state);
member this.BeginSendTo : byte[] * int * int * System.Net.Sockets.SocketFlags * System.Net.EndPoint * AsyncCallback * obj -> IAsyncResult
Public Function BeginSendTo (buffer As Byte(), offset As Integer, size As Integer, socketFlags As SocketFlags, remoteEP As EndPoint, callback As AsyncCallback, state As Object) As IAsyncResult

Parameters

buffer
Byte[]

An array of type Byte that contains the data to send.

offset
Int32

The zero-based position in buffer at which to begin sending data.

size
Int32

The number of bytes to send.

socketFlags
SocketFlags

A bitwise combination of the SocketFlags values.

remoteEP
EndPoint

An EndPoint that represents the remote device.

callback
AsyncCallback

The AsyncCallback delegate.

state
Object

An object that contains state information for this request.

Returns

An IAsyncResult that references the asynchronous send.

Exceptions

buffer is null.

-or-

remoteEP is null.

.NET Framework and .NET 5 and earlier only: An error occurred when attempting to access the socket.

offset is less than 0.

-or-

offset is greater than the length of buffer.

-or-

size is less than 0.

-or-

size is greater than the length of buffer minus the value of the offset parameter.

The Socket has been closed.

A caller higher in the call stack does not have permission for the requested operation.

Remarks

Important

This is a compatibility API. We don't recommend using the APM (Begin* and End*) methods for new development. Instead, use the Task-based equivalents.

You can pass a callback that implements AsyncCallback to BeginSendTo in order to get notified about the completion of the operation. Note that if the underlying network stack completes the operation synchronously, the callback will be executed inline, during the call to BeginSendTo. In this case, the CompletedSynchronously property on the returned IAsyncResult will be set to true to indicate that the method completed synchronously. Use the AsyncState property of the IAsyncResult to obtain the state object passed to the BeginSendTo method.

The BeginSendTo operation must be completed by calling the EndSendTo method. Typically, the method is invoked by the provided AsyncCallback delegate. EndSendTo will block the calling thread until the operation is completed.

If you are using a connection-oriented protocol, you must first call the Connect, BeginConnect, Accept, or BeginAccept method, or BeginSendTo will throw a SocketException. BeginSendTo will ignore the remoteEP parameter and send data to the EndPoint established in the Connect, BeginConnect, Accept, or BeginAccept method.

If you are using a connectionless protocol, you do not need to establish a default remote host with the Connect or BeginConnect method prior to calling SendTo. You only need to do this if you intend to call the BeginSend method. If you do call the Connect or BeginConnect method prior to calling SendTo, the remoteEP parameter will override the specified default remote host for that send operation only. You are also not required to call the Bind 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 LocalEndPoint property after the EndSendTo method successfully completes.

If you want to send data to a broadcast address, you must first call the SetSocketOption 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 EndSendTo will throw a SocketException.

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

Note

If you receive a 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 for a detailed description of the error.

Note

This member outputs trace information when you enable network tracing in your application. For more information, see Network Tracing in .NET Framework.

Note

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

Applies to

See also