語言

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
來源:
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
來源:
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
來源:
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 方法超載。

關於使用非同步程式設計模型的詳細資訊,請參見 「非同步呼叫同步方法」。

適用於