Редагувати

Поділитися через


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(ReadOnlyMemory<Byte>, CancellationToken)

Sends a UDP datagram asynchronously to a remote host.

SendAsync(Byte[], Int32, IPEndPoint)

Sends a UDP datagram asynchronously to a remote host.

SendAsync(ReadOnlyMemory<Byte>, IPEndPoint, CancellationToken)

Sends a UDP datagram asynchronously to a remote host.

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

Sends a UDP datagram asynchronously to a remote host.

SendAsync(ReadOnlyMemory<Byte>, String, Int32, CancellationToken)

Sends a UDP datagram asynchronously to a remote host.

SendAsync(Byte[], Int32)

Source:
UDPClient.cs
Source:
UDPClient.cs
Source:
UDPClient.cs

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

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.

This method stores in the task it returns all non-usage exceptions that the method's synchronous counterpart can throw. If an exception is stored into the returned task, that exception will be thrown when the task is awaited. Usage exceptions, such as ArgumentException, are still thrown synchronously. For the stored exceptions, see the exceptions thrown by Send(Byte[], Int32).

Applies to

SendAsync(ReadOnlyMemory<Byte>, CancellationToken)

Source:
UDPClient.cs
Source:
UDPClient.cs
Source:
UDPClient.cs

Sends a UDP datagram asynchronously to a remote host.

public System.Threading.Tasks.ValueTask<int> SendAsync (ReadOnlyMemory<byte> datagram, System.Threading.CancellationToken cancellationToken = default);
member this.SendAsync : ReadOnlyMemory<byte> * System.Threading.CancellationToken -> System.Threading.Tasks.ValueTask<int>
Public Function SendAsync (datagram As ReadOnlyMemory(Of Byte), Optional cancellationToken As CancellationToken = Nothing) As ValueTask(Of Integer)

Parameters

datagram
ReadOnlyMemory<Byte>

An ReadOnlyMemory<T> of Type Byte that specifies the UDP datagram that you intend to send.

cancellationToken
CancellationToken

The token to monitor for cancellation requests. The default value is None.

Returns

A ValueTask<TResult> that represents the asynchronous send operation. The value of its Result property contains the number of bytes sent.

Exceptions

An error occurred when accessing the socket.

The cancellation token was canceled. This exception is stored into the returned task.

Applies to

SendAsync(Byte[], Int32, IPEndPoint)

Source:
UDPClient.cs
Source:
UDPClient.cs
Source:
UDPClient.cs

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

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.

This method stores in the task it returns all non-usage exceptions that the method's synchronous counterpart can throw. If an exception is stored into the returned task, that exception will be thrown when the task is awaited. Usage exceptions, such as ArgumentException, are still thrown synchronously. For the stored exceptions, see the exceptions thrown by Send(Byte[], Int32, IPEndPoint).

Applies to

SendAsync(ReadOnlyMemory<Byte>, IPEndPoint, CancellationToken)

Source:
UDPClient.cs
Source:
UDPClient.cs
Source:
UDPClient.cs

Sends a UDP datagram asynchronously to a remote host.

public System.Threading.Tasks.ValueTask<int> SendAsync (ReadOnlyMemory<byte> datagram, System.Net.IPEndPoint? endPoint, System.Threading.CancellationToken cancellationToken = default);
member this.SendAsync : ReadOnlyMemory<byte> * System.Net.IPEndPoint * System.Threading.CancellationToken -> System.Threading.Tasks.ValueTask<int>
Public Function SendAsync (datagram As ReadOnlyMemory(Of Byte), endPoint As IPEndPoint, Optional cancellationToken As CancellationToken = Nothing) As ValueTask(Of Integer)

Parameters

datagram
ReadOnlyMemory<Byte>

An ReadOnlyMemory<T> of Type Byte that specifies the UDP datagram that you intend to send.

endPoint
IPEndPoint

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

cancellationToken
CancellationToken

The token to monitor for cancellation requests. The default value is None.

Returns

A ValueTask<TResult> that represents the asynchronous send operation. The value of its Result property contains the number of bytes sent.

Exceptions

UdpClient has already established a default remote host and endPoint is not null.

An error occurred when accessing the socket.

The cancellation token was canceled. This exception is stored into the returned task.

Applies to

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

Source:
UDPClient.cs
Source:
UDPClient.cs
Source:
UDPClient.cs

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

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.

This method stores in the task it returns all non-usage exceptions that the method's synchronous counterpart can throw. If an exception is stored into the returned task, that exception will be thrown when the task is awaited. Usage exceptions, such as ArgumentException, are still thrown synchronously. For the stored exceptions, see the exceptions thrown by Send(Byte[], Int32, String, Int32).

Applies to

SendAsync(ReadOnlyMemory<Byte>, String, Int32, CancellationToken)

Source:
UDPClient.cs
Source:
UDPClient.cs
Source:
UDPClient.cs

Sends a UDP datagram asynchronously to a remote host.

public System.Threading.Tasks.ValueTask<int> SendAsync (ReadOnlyMemory<byte> datagram, string? hostname, int port, System.Threading.CancellationToken cancellationToken = default);
member this.SendAsync : ReadOnlyMemory<byte> * string * int * System.Threading.CancellationToken -> System.Threading.Tasks.ValueTask<int>
Public Function SendAsync (datagram As ReadOnlyMemory(Of Byte), hostname As String, port As Integer, Optional cancellationToken As CancellationToken = Nothing) As ValueTask(Of Integer)

Parameters

datagram
ReadOnlyMemory<Byte>

An ReadOnlyMemory<T> of Type Byte that specifies the UDP datagram that you intend to send.

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.

cancellationToken
CancellationToken

The token to monitor for cancellation requests. The default value is None.

Returns

A ValueTask<TResult> that represents the asynchronous send operation. The value of its Result property contains the number of bytes sent.

Exceptions

The UdpClient has already established a default remote host.

An error occurred when accessing the socket.

The cancellation token was canceled. This exception is stored into the returned task.

Applies to