System.Net.Sockets.UdpClient.SendAsync Method

Sends a UDP datagram asynchronously to a remote host.

Syntax

public System.Threading.Tasks.Task<int> SendAsync (byte[] datagram, int bytes, string hostname, int port)

Parameters

datagram
An array of type byte that specifies the UDP datagram that you intend to send represented as an array of bytes.
bytes
The number of bytes in the datagram.
hostname
The name of the remote host to which you intend to send the datagram.
port
The remote port number with which you intend to communicate.

Returns

Returns System.Threading.Tasks.Task`1.

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 UdpClient.Client method to obtain the underlying System.Net.Sockets.Socket, and set the socket option to SocketOptionName.Broadcast. You can also revert to using the System.Net.Sockets.Socket class.

Note:

Do not provide a host name or port number to this method if you have already established a remote host with the UdpClient.Connect(string, int) method. If you do, this method will throw a System.Net.Sockets.SocketException. If you receive a System.Net.Sockets.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 in MSDN for a detailed description of the error.

Requirements

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