UdpClient.SendAsync UdpClient.SendAsync UdpClient.SendAsync UdpClient.SendAsync Method

定義

將 UDP 資料包非同步傳送至遠端主機。Sends a UDP datagram asynchronously to a remote host.

多載

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

將 UDP 資料包非同步傳送至遠端主機。Sends a UDP datagram asynchronously to a remote host.

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

將 UDP 資料包非同步傳送至遠端主機。Sends a UDP datagram asynchronously to a remote host.

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

將 UDP 資料包非同步傳送至遠端主機。Sends a UDP datagram asynchronously to a remote host.

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

將 UDP 資料包非同步傳送至遠端主機。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)

參數

datagram
Byte[]

Byte 型別的陣列,指定您想傳送的 UDP 資料包 (表示為位元組陣列)。An array of type Byte that specifies the UDP datagram that you intend to send represented as an array of bytes.

bytes
Int32 Int32 Int32 Int32

資料包中的位元組數目。The number of bytes in the datagram.

傳回

傳回 Task<TResult>Returns Task<TResult>.

例外狀況

UdpClient 已建立預設遠端主機。The UdpClient has already established a default remote host.

存取通訊端時發生錯誤。An error occurred when accessing the socket.

備註

這個多載會將資料包傳送到在Connect方法中建立的遠端主機。This overload sends datagrams to the remote host established in the Connect method. 如果您未在呼叫Connect這個多載之前呼叫, 方法將會SocketException擲回。If you do not call Connect before calling this overload, the method will throw a SocketException. 如果您收到SocketException, 請使用SocketException.ErrorCode來取得特定的錯誤碼。If you receive a SocketException, use SocketException.ErrorCode to obtain the specific error code. 取得此程式碼之後, 您可以參考Windows socket 第2版 API 錯誤碼檔, 以取得錯誤的詳細描述。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.

如果您想要將資料包傳送至不同的遠端主機, 您必須Connect呼叫方法, 並指定所需的遠端主機。If you want to send datagrams to a different remote host, you must call the Connect method and specify the desired remote host. 使用任何其他SendAsync方法多載, 將資料包傳送至廣播位址。Use either of the other SendAsync method overloads to send datagrams to a broadcast address.

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

將 UDP 資料包非同步傳送至遠端主機。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);
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)

參數

datagram
Byte[]

Byte 型別的陣列,指定您想傳送的 UDP 資料包 (表示為位元組陣列)。An array of type Byte that specifies the UDP datagram that you intend to send represented as an array of bytes.

bytes
Int32 Int32 Int32 Int32

資料包中的位元組數目。The number of bytes in the datagram.

endPoint
IPEndPoint IPEndPoint IPEndPoint IPEndPoint

IPEndPoint,表示傳送資料包的目標主機和通訊埠。An IPEndPoint that represents the host and port to which to send the datagram.

傳回

傳回 Task<TResult>Returns Task<TResult>.

例外狀況

UdpClient 已建立預設遠端主機。UdpClient has already established a default remote host.

存取通訊端時發生錯誤。An error occurred when accessing the socket.

備註

這個方法會將資料包傳送至指定的端點。This method sends datagrams to the specified endpoint. 呼叫此多載之前, 您必須先IPEndPoint使用要傳遞資料包的遠端主機 IP 位址和埠號碼, 來建立。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. 您可以藉由指定SocketOptionName.Broadcast Address的屬性IPEndPoint, 將資料包傳送到預設廣播位址 (255.255.255.255)。You can send datagrams to the default broadcast address, 255.255.255.255, by specifying SocketOptionName.Broadcast for the Address property of the IPEndPoint. 建立此IPEndPoint檔案之後, 請將它傳遞給這個方法endPoint做為參數。After you have created this IPEndPoint, pass it to this method as the endPoint parameter.

如果您想要將資料包傳送至任何其他廣播位址, Client請使用方法來取得Socket基礎, 並將通訊端選項SocketOptionName.Broadcast設定為。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. 您也可以使用Socket類別來還原為。You can also revert to using the Socket class.

警告

如果您已經Connect使用endPoint方法建立遠端主機, 請勿提供此方法的參數。Do not provide an endPoint parameter to this method if you have already established a remote host with the Connect method. 如果您這樣做, 這個方法將會SocketException擲回。If you do, this method will throw a SocketException. 如果您收到SocketException, 請使用SocketException.ErrorCode來取得特定的錯誤碼。If you receive a SocketException, use SocketException.ErrorCode to obtain the specific error code. 取得此程式碼之後, 您可以參考Windows socket 第2版 API 錯誤碼檔, 以取得錯誤的詳細描述。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.

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

將 UDP 資料包非同步傳送至遠端主機。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);
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)

參數

datagram
Byte[]

Byte 型別的陣列,指定您想傳送的 UDP 資料包 (表示為位元組陣列)。An array of type Byte that specifies the UDP datagram that you intend to send represented as an array of bytes.

bytes
Int32 Int32 Int32 Int32

資料包中的位元組數目。The number of bytes in the datagram.

hostname
String String String String

您想傳送資料包所至的遠端主機名稱。The name of the remote host to which you intend to send the datagram.

port
Int32 Int32 Int32 Int32

您要通訊的遠端通訊埠編號。The remote port number with which you intend to communicate.

傳回

傳回 Task<TResult>Returns Task<TResult>.

例外狀況

UdpClient 已建立預設遠端主機。The UdpClient has already established a default remote host.

存取通訊端時發生錯誤。An error occurred when accessing the socket.

備註

這個方法會將資料包傳送至hostnameport參數所指定的值。This method sends datagrams to the values specified by the hostname and port parameters. 您可以指定 "255.255.255.255" 作為hostname參數值, 以將資料包傳送到預設廣播位址。You can send datagrams to the default broadcast address by specifying "255.255.255.255" for the hostname parameter value.

如果您想要將資料包傳送至任何其他廣播位址, Client請使用方法來取得Socket基礎, 並將通訊端選項SocketOptionName.Broadcast設定為。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. 您也可以使用Socket類別來還原為。You can also revert to using the Socket class.

警告

如果您已經使用Connect方法建立遠端主機, 請勿提供主機名稱或埠號碼給這個方法。Do not provide a host name or port number to this method if you have already established a remote host with the Connect method. 如果您這樣做, 這個方法將會SocketException擲回。If you do, this method will throw a SocketException. 如果您收到SocketException, 請使用SocketException.ErrorCode來取得特定的錯誤碼。If you receive a SocketException, use SocketException.ErrorCode to obtain the specific error code. 取得此程式碼之後, 您可以參考Windows socket 第2版 API 錯誤碼檔, 以取得錯誤的詳細描述。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.

適用於