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

.NET Framework (current version)
 
System_CAPS_noteNote

The .NET API Reference documentation has a new home. Visit the .NET API Browser on docs.microsoft.com to see the new experience.

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,
	string hostname,
	int port
)

Parameters

datagram
Type: System.Byte[]

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

bytes
Type: System.Int32

The number of bytes in the datagram.

hostname
Type: System.String

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

port
Type: System.Int32

The remote port number with which you intend to communicate.

Return Value

Type: System.Threading.Tasks.Task<Int32>

Returns Task<TResult>.

Exception Condition
ArgumentNullException

dgram is null.

InvalidOperationException

The UdpClient has already established a default remote host.

ObjectDisposedException

The UdpClient is closed.

SocketException

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

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.

System_CAPS_warningWarning

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

.NET Framework
Available since 4.5
Return to top
Show: