UdpClient.BeginReceive(AsyncCallback, Object) UdpClient.BeginReceive(AsyncCallback, Object) UdpClient.BeginReceive(AsyncCallback, Object) UdpClient.BeginReceive(AsyncCallback, Object) Method

定義

非同步接收遠端主機的資料包。Receives a datagram from a remote host asynchronously.

public:
 IAsyncResult ^ BeginReceive(AsyncCallback ^ requestCallback, System::Object ^ state);
public IAsyncResult BeginReceive (AsyncCallback requestCallback, object state);
member this.BeginReceive : AsyncCallback * obj -> IAsyncResult
Public Function BeginReceive (requestCallback As AsyncCallback, state As Object) As IAsyncResult

參數

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 receive operation. 作業完成時會將這個物件傳遞至 requestCallback 委派。This object is passed to the requestCallback delegate when the operation is complete.

傳回

IAsyncResult 物件,參考非同步接收。An IAsyncResult object that references the asynchronous receive.

範例

下列程式碼範例會BeginReceive使用來以非同步方式接收伺服器回應。The following code example uses BeginReceive to asynchronously receive a server response.

private:
    static int listenPort = 13000;

public:
    value struct UdpState
    {
    public:
        UdpClient^ udpClient;
        IPEndPoint^ ipEndPoint;
    };

    static bool isMessageReceived;

    static void ReceiveCallback(IAsyncResult^ asyncResult)
    {
        UdpClient^ udpClient =
            ((UdpState)(asyncResult->AsyncState)).udpClient;
        IPEndPoint^ ipEndPoint =
            ((UdpState)(asyncResult->AsyncState)).ipEndPoint;

        array<Byte>^ receiveBytes =
            udpClient->EndReceive(asyncResult, ipEndPoint);
        String^ receiveString =
            Encoding::ASCII->GetString(receiveBytes);

        Console::WriteLine("Received: {0}", receiveString);
        isMessageReceived = true;
    }

    static void ReceiveMessages()
    {
        // Receive a message and write it to the console.
        IPEndPoint^ ipEndPoint = gcnew IPEndPoint(IPAddress::Any, listenPort);
        UdpClient^ udpClient = gcnew UdpClient(ipEndPoint);

        UdpState^ udpState = gcnew UdpState();
        udpState->ipEndPoint = ipEndPoint;
        udpState->udpClient = udpClient;

        Console::WriteLine("listening for messages");
        udpClient->BeginReceive(gcnew AsyncCallback(ReceiveCallback),
            udpState);

        // Do some work while we wait for a message. For this example,
        // we'll just sleep
        while (!isMessageReceived)
        {
            Thread::Sleep(100);
        }
    }
public struct UdpState
{
    public UdpClient u;
    public IPEndPoint e;
}

public static bool messageReceived = false;

public static void ReceiveCallback(IAsyncResult ar)
{
    UdpClient u = ((UdpState)(ar.AsyncState)).u;
    IPEndPoint e = ((UdpState)(ar.AsyncState)).e;

    byte[] receiveBytes = u.EndReceive(ar, ref e);
    string receiveString = Encoding.ASCII.GetString(receiveBytes);

    Console.WriteLine($"Received: {receiveString}");
    messageReceived = true;
}

public static void ReceiveMessages()
{
    // Receive a message and write it to the console.
    IPEndPoint e = new IPEndPoint(IPAddress.Any, s_listenPort);
    UdpClient u = new UdpClient(e);

    UdpState s = new UdpState();
    s.e = e;
    s.u = u;

    Console.WriteLine("listening for messages");
    u.BeginReceive(new AsyncCallback(ReceiveCallback), s);

    // Do some work while we wait for a message. For this example, we'll just sleep
    while (!messageReceived)
    {
        Thread.Sleep(100);
    }
}

備註

非同步BeginReceive操作必須藉由EndReceive呼叫方法來完成。The asynchronous BeginReceive operation must be completed by calling the EndReceive method. 通常, 方法是由requestCallback委派叫用。Typically, the method is invoked by the requestCallback delegate.

在作業完成之前, 這個方法不會封鎖。This method does not block until the operation is complete. 若要封鎖直到作業完成, 請使用Receive方法。To block until the operation is complete, use the Receive method.

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

適用於