Export (0) Print
Expand All

UdpClient.SendAsync Method (Byte[], Int32, IPEndPoint)

.NET Framework 4.5

Sends a UDP datagram asynchronously to a remote host.

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

[HostProtectionAttribute(SecurityAction.LinkDemand, ExternalThreading = true)]
public Task<int> SendAsync(
	byte[] datagram,
	int bytes,
	IPEndPoint endPoint


Type: System.Byte[]

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

Type: System.Int32

The number of bytes in the datagram.

Type: System.Net.IPEndPoint

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


dgram is null.


UdpClient has already established a default remote host.


UdpClient is closed.


An error occurred when accessing the socket. See the Remarks section for more information.

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,, 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.

Caution noteCaution

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 in MSDN for a detailed description of the error.


The HostProtectionAttribute attribute applied to this type or member has the following Resources property value: ExternalThreading. The HostProtectionAttribute does not affect desktop applications (which are typically started by double-clicking an icon, typing a command, or entering a URL in a browser). For more information, see the HostProtectionAttribute class or SQL Server Programming and Host Protection Attributes.

.NET Framework

Supported in: 4.5

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

© 2014 Microsoft