UdpClient.BeginSend UdpClient.BeginSend UdpClient.BeginSend UdpClient.BeginSend Method

定義

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

多載

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

將資料包非同步傳送至目的端。Sends a datagram to a destination asynchronously. 目的端由主機名稱和通訊埠編號指定。The destination is specified by the host name and port number.

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

將資料包非同步傳送至遠端主機。Sends a datagram to a remote host asynchronously. 目的端由先前對 Connect 的呼叫指定。The destination was specified previously by a call to Connect.

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

將資料包非同步傳送至目的端。Sends a datagram to a destination asynchronously. 目的端由 EndPoint 指定。The destination is specified by a EndPoint.

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

將資料包非同步傳送至目的端。Sends a datagram to a destination asynchronously. 目的端由主機名稱和通訊埠編號指定。The destination is specified by the host name and port number.

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);
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 陣列,包含要傳送的資料。A Byte array that contains the data to be sent.

bytes
Int32 Int32 Int32 Int32

要傳送的位元組數。The number of bytes to send.

hostname
String String String String

目的主機。The destination host.

port
Int32 Int32 Int32 Int32

目的通訊埠編號。The destination port number.

requestCallback
AsyncCallback AsyncCallback AsyncCallback AsyncCallback

AsyncCallback 委派,會於作業完成時參考要叫用的方法。An AsyncCallback delegate that references the method to invoke when the operation is complete.

state
Object Object Object Object

包含傳送作業相關資訊的使用者定義物件。A user-defined object that contains information about the send operation. 作業完成時會將這個物件傳遞至 requestCallback 委派。This object is passed to the requestCallback delegate when the operation is complete.

傳回

IAsyncResult 物件,參考非同步傳送。An IAsyncResult object that references the asynchronous send.

範例

下列程式碼範例會BeginSend使用來以非同步方式傳送伺服器要求。The following code example uses BeginSend to asynchronously send a server request.

public:
    static bool isMessageSent;

    static void SendCallback(IAsyncResult^ asyncResult)
    {
        UdpClient^ udpClient = (UdpClient^)asyncResult->AsyncState;

        Console::WriteLine("number of bytes sent: {0}",
            udpClient->EndSend(asyncResult));
        isMessageSent = true;
    }
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;
}
public:
    static void SendMessage3(String^ server, String^ message)
    {
        // create the udp socket
        UdpClient^ udpClient = gcnew UdpClient();

        array<Byte>^ sendBytes = Encoding::ASCII->GetBytes(message);

        // send the message
        // the destination is defined by the server name and port
        udpClient->BeginSend(sendBytes, sendBytes->Length, server, listenPort,
            gcnew AsyncCallback(SendCallback), udpClient);

        // Do some work while we wait for the send to complete. For
        // this example, we'll just sleep
        while (!isMessageSent)
        {
            Thread::Sleep(100);
        }
    }
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呼叫方法來完成。The asynchronous BeginSend operation must be completed by calling the EndSend method. 通常, 方法是由requestCallback委派叫用。Typically, the method is invoked by the requestCallback delegate.

在作業完成之前, 這個方法不會封鎖。This method does not block until the operation is complete. 若要封鎖直到作業完成, 請使用其中一個Send方法多載。To block until the operation is complete, use one of the Send method overloads.

如需使用非同步程式設計模型的詳細資訊, 請參閱以非同步方式呼叫同步方法For detailed information about using the asynchronous programming model, see Calling Synchronous Methods Asynchronously.

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

將資料包非同步傳送至遠端主機。Sends a datagram to a remote host asynchronously. 目的端由先前對 Connect 的呼叫指定。The destination was specified previously by a call to 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);
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 陣列,包含要傳送的資料。A Byte array that contains the data to be sent.

bytes
Int32 Int32 Int32 Int32

要傳送的位元組數。The number of bytes to send.

requestCallback
AsyncCallback AsyncCallback AsyncCallback AsyncCallback

AsyncCallback 委派,會於作業完成時參考要叫用的方法。An AsyncCallback delegate that references the method to invoke when the operation is complete.

state
Object Object Object Object

包含傳送作業相關資訊的使用者定義物件。A user-defined object that contains information about the send operation. 作業完成時會將這個物件傳遞至 requestCallback 委派。This object is passed to the requestCallback delegate when the operation is complete.

傳回

IAsyncResult 物件,參考非同步傳送。An IAsyncResult object that references the asynchronous send.

範例

下列程式碼範例會BeginSend使用來以非同步方式傳送伺服器要求。The following code example uses BeginSend to asynchronously send a server request.

public:
    static bool isMessageSent;

    static void SendCallback(IAsyncResult^ asyncResult)
    {
        UdpClient^ udpClient = (UdpClient^)asyncResult->AsyncState;

        Console::WriteLine("number of bytes sent: {0}",
            udpClient->EndSend(asyncResult));
        isMessageSent = true;
    }
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;
}
public:
    static void SendMessage1(String^ server, String^ message)
    {
        // create the udp socket
        UdpClient^ udpClient = gcnew UdpClient();

        udpClient->Connect(server, listenPort);
        array<Byte>^ sendBytes = Encoding::ASCII->GetBytes(message);

        // send the message
        // the destination is defined by the call to .Connect()
        udpClient->BeginSend(sendBytes, sendBytes->Length,
            gcnew AsyncCallback(SendCallback), udpClient);

        // Do some work while we wait for the send to complete. For
        // this example, we'll just sleep
        while (!isMessageSent)
        {
            Thread::Sleep(100);
        }
    }
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呼叫方法來完成。The asynchronous BeginSend operation must be completed by calling the EndSend method. 通常, 方法是由requestCallback委派叫用。Typically, the method is invoked by the requestCallback delegate.

在作業完成之前, 這個方法不會封鎖。This method does not block until the operation completes. 若要封鎖直到作業完成, 請使用其中一個Send方法多載。To block until the operation is complete, use one of the Send method overloads.

如需使用非同步程式設計模型的詳細資訊, 請參閱以非同步方式呼叫同步方法For detailed information about using the asynchronous programming model, see Calling Synchronous Methods Asynchronously.

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

將資料包非同步傳送至目的端。Sends a datagram to a destination asynchronously. 目的端由 EndPoint 指定。The destination is specified by a 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);
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 陣列,包含要傳送的資料。A Byte array that contains the data to be sent.

bytes
Int32 Int32 Int32 Int32

要傳送的位元組數。The number of bytes to send.

endPoint
IPEndPoint IPEndPoint IPEndPoint IPEndPoint

EndPoint,表示資料的目的端。The EndPoint that represents the destination for the data.

requestCallback
AsyncCallback AsyncCallback AsyncCallback AsyncCallback

AsyncCallback 委派,會於作業完成時參考要叫用的方法。An AsyncCallback delegate that references the method to invoke when the operation is complete.

state
Object Object Object Object

包含傳送作業相關資訊的使用者定義物件。A user-defined object that contains information about the send operation. 作業完成時會將這個物件傳遞至 requestCallback 委派。This object is passed to the requestCallback delegate when the operation is complete.

傳回

IAsyncResult 物件,參考非同步傳送。An IAsyncResult object that references the asynchronous send.

範例

下列程式碼範例會BeginSend使用來以非同步方式傳送伺服器要求。The following code example uses BeginSend to asynchronously send a server request.

public:
    static bool isMessageSent;

    static void SendCallback(IAsyncResult^ asyncResult)
    {
        UdpClient^ udpClient = (UdpClient^)asyncResult->AsyncState;

        Console::WriteLine("number of bytes sent: {0}",
            udpClient->EndSend(asyncResult));
        isMessageSent = true;
    }
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;
}
public:
    static void SendMessage2(String^ server, String^ message)
    {
        // create the udp socket
        UdpClient^ udpClient = gcnew UdpClient();
        array<Byte>^ sendBytes = Encoding::ASCII->GetBytes(message);

        // resolve the server name
        IPHostEntry^ resolvedServer = Dns::GetHostEntry(server);

        IPEndPoint^ ipEndPoint =
            gcnew IPEndPoint(resolvedServer->AddressList[0], listenPort);

        // send the message
        // the destination is defined by the IPEndPoint
        udpClient->BeginSend(sendBytes, sendBytes->Length, ipEndPoint,
            gcnew AsyncCallback(SendCallback), udpClient);

        // Do some work while we wait for the send to complete. For
        // this example, we'll just sleep
        while (!isMessageSent)
        {
            Thread::Sleep(100);
        }
    }
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呼叫方法來完成。The asynchronous BeginSend operation must be completed by calling the EndSend method. 通常, 方法是由requestCallback委派叫用。Typically, the method is invoked by the requestCallback delegate.

在作業完成之前, 這個方法不會封鎖。This method does not block until the operation is complete. 若要封鎖直到作業完成, 請使用其中一個Send方法多載。To block until the operation is complete, use one of the Send method overloads.

如需使用非同步程式設計模型的詳細資訊, 請參閱以非同步方式呼叫同步方法For detailed information about using the asynchronous programming model, see Calling Synchronous Methods Asynchronously.

適用於