共用方式為


UdpClient.BeginSend 方法

定義

將資料包非同步傳送至遠端主機。

多載

名稱 Description
BeginSend(Byte[], Int32, String, Int32, AsyncCallback, Object)

將資料包非同步傳送至目的端。 目的端由主機名稱和通訊埠編號指定。

BeginSend(Byte[], Int32, IPEndPoint, AsyncCallback, Object)

將資料包非同步傳送至目的端。 目的端由 EndPoint 指定。

BeginSend(Byte[], Int32, AsyncCallback, Object)

將資料包非同步傳送至遠端主機。 目的端由先前對 Connect 的呼叫指定。

BeginSend(Byte[], Int32, String, Int32, AsyncCallback, Object)

來源:
UDPClient.cs
來源:
UDPClient.cs
來源:
UDPClient.cs
來源:
UDPClient.cs

將資料包非同步傳送至目的端。 目的端由主機名稱和通訊埠編號指定。

public:
 IAsyncResult ^ BeginSend(cli::array <System::Byte> ^ datagram, int bytes, System::String ^ hostname, int port, AsyncCallback ^ requestCallback, System::Object ^ state);
public IAsyncResult BeginSend(byte[] datagram, int bytes, string? hostname, int port, AsyncCallback? requestCallback, object? state);
public IAsyncResult BeginSend(byte[] datagram, int bytes, string hostname, int port, AsyncCallback requestCallback, object state);
member this.BeginSend : byte[] * int * string * int * AsyncCallback * obj -> IAsyncResult
Public Function BeginSend (datagram As Byte(), bytes As Integer, hostname As String, port As Integer, requestCallback As AsyncCallback, state As Object) As IAsyncResult

參數

datagram
Byte[]

Byte 陣列,包含要傳送的資料。

bytes
Int32

要傳送的位元組數。

hostname
String

目的主機。

port
Int32

目的通訊埠編號。

requestCallback
AsyncCallback

AsyncCallback 委派,會於作業完成時參考要叫用的方法。

state
Object

包含傳送作業相關資訊的使用者定義物件。 作業完成時會將這個物件傳遞至 requestCallback 委派。

傳回

IAsyncResult 物件,參考非同步傳送。

範例

下列程式代碼範例會使用 BeginSend 來異步傳送伺服器要求。

public static bool messageSent = false;

public static void SendCallback(IAsyncResult ar)
{
    UdpClient u = (UdpClient)ar.AsyncState;

    Console.WriteLine($"number of bytes sent: {u.EndSend(ar)}");
    messageSent = true;
}
static void SendMessage3(string server, string message)
{
    // create the udp socket
    UdpClient u = new UdpClient();

    byte[] sendBytes = Encoding.ASCII.GetBytes(message);

    // send the message
    // the destination is defined by the server name and port
    u.BeginSend(sendBytes, sendBytes.Length, server, s_listenPort, new AsyncCallback(SendCallback), u);

    // Do some work while we wait for the send to complete. For this example, we'll just sleep
    while (!messageSent)
    {
        Thread.Sleep(100);
    }
}

備註

異步 BeginSend 操作必須藉由呼叫 EndSend 方法來完成。 一般而言,委派會叫 requestCallback 用 方法。

在作業完成之前,此方法不會封鎖。 若要封鎖直到作業完成為止,請使用其中 Send 一個方法多載。

如需使用異步程序設計模型的詳細資訊,請參閱 異步呼叫同步方法

適用於

BeginSend(Byte[], Int32, IPEndPoint, AsyncCallback, Object)

來源:
UDPClient.cs
來源:
UDPClient.cs
來源:
UDPClient.cs
來源:
UDPClient.cs

將資料包非同步傳送至目的端。 目的端由 EndPoint 指定。

public:
 IAsyncResult ^ BeginSend(cli::array <System::Byte> ^ datagram, int bytes, System::Net::IPEndPoint ^ endPoint, AsyncCallback ^ requestCallback, System::Object ^ state);
public IAsyncResult BeginSend(byte[] datagram, int bytes, System.Net.IPEndPoint? endPoint, AsyncCallback? requestCallback, object? state);
public IAsyncResult BeginSend(byte[] datagram, int bytes, System.Net.IPEndPoint endPoint, AsyncCallback requestCallback, object state);
member this.BeginSend : byte[] * int * System.Net.IPEndPoint * AsyncCallback * obj -> IAsyncResult
Public Function BeginSend (datagram As Byte(), bytes As Integer, endPoint As IPEndPoint, requestCallback As AsyncCallback, state As Object) As IAsyncResult

參數

datagram
Byte[]

Byte 陣列,包含要傳送的資料。

bytes
Int32

要傳送的位元組數。

endPoint
IPEndPoint

EndPoint,表示資料的目的端。

requestCallback
AsyncCallback

AsyncCallback 委派,會於作業完成時參考要叫用的方法。

state
Object

包含傳送作業相關資訊的使用者定義物件。 作業完成時會將這個物件傳遞至 requestCallback 委派。

傳回

IAsyncResult 物件,參考非同步傳送。

範例

下列程式代碼範例會使用 BeginSend 來異步傳送伺服器要求。

public static bool messageSent = false;

public static void SendCallback(IAsyncResult ar)
{
    UdpClient u = (UdpClient)ar.AsyncState;

    Console.WriteLine($"number of bytes sent: {u.EndSend(ar)}");
    messageSent = true;
}
static void SendMessage2(string server, string message)
{
    // create the udp socket
    UdpClient u = new UdpClient();
    byte[] sendBytes = Encoding.ASCII.GetBytes(message);

    // resolve the server name
    IPHostEntry heserver = Dns.GetHostEntry(server);

    IPEndPoint e = new IPEndPoint(heserver.AddressList[0], s_listenPort);

    // send the message
    // the destination is defined by the IPEndPoint
    u.BeginSend(sendBytes, sendBytes.Length, e, new AsyncCallback(SendCallback), u);

    // Do some work while we wait for the send to complete. For this example, we'll just sleep
    while (!messageSent)
    {
        Thread.Sleep(100);
    }
}

備註

異步 BeginSend 操作必須藉由呼叫 EndSend 方法來完成。 一般而言,委派會叫 requestCallback 用 方法。

在作業完成之前,此方法不會封鎖。 若要封鎖直到作業完成為止,請使用其中 Send 一個方法多載。

如需使用異步程序設計模型的詳細資訊,請參閱 異步呼叫同步方法

適用於

BeginSend(Byte[], Int32, AsyncCallback, Object)

來源:
UDPClient.cs
來源:
UDPClient.cs
來源:
UDPClient.cs
來源:
UDPClient.cs

將資料包非同步傳送至遠端主機。 目的端由先前對 Connect 的呼叫指定。

public:
 IAsyncResult ^ BeginSend(cli::array <System::Byte> ^ datagram, int bytes, AsyncCallback ^ requestCallback, System::Object ^ state);
public IAsyncResult BeginSend(byte[] datagram, int bytes, AsyncCallback? requestCallback, object? state);
public IAsyncResult BeginSend(byte[] datagram, int bytes, AsyncCallback requestCallback, object state);
member this.BeginSend : byte[] * int * AsyncCallback * obj -> IAsyncResult
Public Function BeginSend (datagram As Byte(), bytes As Integer, requestCallback As AsyncCallback, state As Object) As IAsyncResult

參數

datagram
Byte[]

Byte 陣列,包含要傳送的資料。

bytes
Int32

要傳送的位元組數。

requestCallback
AsyncCallback

AsyncCallback 委派,會於作業完成時參考要叫用的方法。

state
Object

包含傳送作業相關資訊的使用者定義物件。 作業完成時會將這個物件傳遞至 requestCallback 委派。

傳回

IAsyncResult 物件,參考非同步傳送。

範例

下列程式代碼範例會使用 BeginSend 來異步傳送伺服器要求。

public static bool messageSent = false;

public static void SendCallback(IAsyncResult ar)
{
    UdpClient u = (UdpClient)ar.AsyncState;

    Console.WriteLine($"number of bytes sent: {u.EndSend(ar)}");
    messageSent = true;
}
static void SendMessage1(string server, string message)
{
    // create the udp socket
    UdpClient u = new UdpClient();

    u.Connect(server, s_listenPort);
    byte[] sendBytes = Encoding.ASCII.GetBytes(message);

    // send the message
    // the destination is defined by the call to .Connect()
    u.BeginSend(sendBytes, sendBytes.Length, new AsyncCallback(SendCallback), u);

    // Do some work while we wait for the send to complete. For this example, we'll just sleep
    while (!messageSent)
    {
        Thread.Sleep(100);
    }
}

備註

異步 BeginSend 操作必須藉由呼叫 EndSend 方法來完成。 一般而言,委派會叫 requestCallback 用 方法。

在作業完成之前,此方法不會封鎖。 若要封鎖直到作業完成為止,請使用其中 Send 一個方法多載。

如需使用異步程序設計模型的詳細資訊,請參閱 異步呼叫同步方法

適用於