UdpClient.SendAsync Method

Definition

Sends a UDP datagram asynchronously to a remote host.

Overloads

SendAsync(Byte[], Int32)

Sends a UDP datagram asynchronously to a remote host.

SendAsync(Byte[], Int32, IPEndPoint)

Sends a UDP datagram asynchronously to a remote host.

SendAsync(Byte[], Int32, String, Int32)

Sends a UDP datagram asynchronously to a remote host.

SendAsync(Byte[], Int32)

Sends a UDP datagram asynchronously to a remote host.

public:
 System::Threading::Tasks::Task<int> ^ SendAsync(cli::array <System::Byte> ^ datagram, int bytes);
public System.Threading.Tasks.Task<int> SendAsync (byte[] datagram, int bytes);
member this.SendAsync : byte[] * int -> System.Threading.Tasks.Task<int>
Public Function SendAsync (datagram As Byte(), bytes As Integer) As Task(Of Integer)

Parameters

datagram
Byte[]

An array of type Byte that specifies the UDP datagram that you intend to send represented as an array of bytes.

bytes
Int32

The number of bytes in the datagram.

Returns

Task<Int32>

Returns Task<TResult>.

Exceptions

dgram is null.

The UdpClient has already established a default remote host.

An error occurred when accessing the socket.

Remarks

This overload sends datagrams to the remote host established in the Connect method. If you do not call Connect before calling this overload, the method will throw a SocketException. If you receive a SocketException, use SocketException.ErrorCode to obtain the specific error code. Once you have obtained this code, you can refer to the Windows Sockets version 2 API error code documentation for a detailed description of the error.

If you want to send datagrams to a different remote host, you must call the Connect method and specify the desired remote host. Use either of the other SendAsync method overloads to send datagrams to a broadcast address.

Applies to

SendAsync(Byte[], Int32, IPEndPoint)

Sends a UDP datagram asynchronously to a remote host.

public:
 System::Threading::Tasks::Task<int> ^ SendAsync(cli::array <System::Byte> ^ datagram, int bytes, System::Net::IPEndPoint ^ endPoint);
public System.Threading.Tasks.Task<int> SendAsync (byte[] datagram, int bytes, System.Net.IPEndPoint? endPoint);
public System.Threading.Tasks.Task<int> SendAsync (byte[] datagram, int bytes, System.Net.IPEndPoint endPoint);
member this.SendAsync : byte[] * int * System.Net.IPEndPoint -> System.Threading.Tasks.Task<int>
Public Function SendAsync (datagram As Byte(), bytes As Integer, endPoint As IPEndPoint) As Task(Of Integer)

Parameters

datagram
Byte[]

An array of type Byte that specifies the UDP datagram that you intend to send represented as an array of bytes.

bytes
Int32

The number of bytes in the datagram.

endPoint
IPEndPoint

An IPEndPoint that represents the host and port to which to send the datagram.

Returns

Task<Int32>

Returns Task<TResult>.

Exceptions

dgram is null.

UdpClient has already established a default remote host.

An error occurred when accessing the socket.

Remarks

This method sends datagrams to the specified endpoint. Before calling this overload, you must first create an IPEndPoint using the IP address and port number of the remote host to which your datagrams will be delivered. You can send datagrams to the default broadcast address, 255.255.255.255, by specifying SocketOptionName.Broadcast for the Address property of the IPEndPoint. After you have created this IPEndPoint, pass it to this method as the endPoint parameter.

If you want to send datagrams to any other broadcast address, use the Client method to obtain the underlying Socket, and set the socket option to SocketOptionName.Broadcast. You can also revert to using the Socket class.

Warning

Do not provide an endPoint parameter to this method if you have already established a remote host with the Connect method. If you do, this method will throw a SocketException. If you receive a SocketException, use SocketException.ErrorCode to obtain the specific error code. Once you have obtained this code, you can refer to the Windows Sockets version 2 API error code documentation for a detailed description of the error.

Applies to

SendAsync(Byte[], Int32, String, Int32)

Sends a UDP datagram asynchronously to a remote host.

public:
 System::Threading::Tasks::Task<int> ^ SendAsync(cli::array <System::Byte> ^ datagram, int bytes, System::String ^ hostname, int port);
public System.Threading.Tasks.Task<int> SendAsync (byte[] datagram, int bytes, string? hostname, int port);
public System.Threading.Tasks.Task<int> SendAsync (byte[] datagram, int bytes, string hostname, int port);
member this.SendAsync : byte[] * int * string * int -> System.Threading.Tasks.Task<int>
Public Function SendAsync (datagram As Byte(), bytes As Integer, hostname As String, port As Integer) As Task(Of Integer)

Parameters

datagram
Byte[]

An array of type Byte that specifies the UDP datagram that you intend to send represented as an array of bytes.

bytes
Int32

The number of bytes in the datagram.

hostname
String

The name of the remote host to which you intend to send the datagram.

port
Int32

The remote port number with which you intend to communicate.

Returns

Task<Int32>

Returns Task<TResult>.

Exceptions

dgram is null.

The UdpClient has already established a default remote host.

An error occurred when accessing the socket.

Remarks

This method sends datagrams to the values specified by the hostname and port parameters. You can send datagrams to the default broadcast address by specifying "255.255.255.255" for the hostname parameter value.

If you want to send datagrams to any other broadcast address, use the Client method to obtain the underlying Socket, and set the socket option to SocketOptionName.Broadcast. You can also revert to using the Socket class.

Warning

Do not provide a host name or port number to this method if you have already established a remote host with the Connect method. If you do, this method will throw a SocketException. If you receive a SocketException, use SocketException.ErrorCode to obtain the specific error code. Once you have obtained this code, you can refer to the Windows Sockets version 2 API error code documentation for a detailed description of the error.

Applies to