UdpClient.EndReceive(IAsyncResult, IPEndPoint) UdpClient.EndReceive(IAsyncResult, IPEndPoint) UdpClient.EndReceive(IAsyncResult, IPEndPoint) UdpClient.EndReceive(IAsyncResult, IPEndPoint) Method

定義

結束暫止的非同步接收。Ends a pending asynchronous receive.

public:
 cli::array <System::Byte> ^ EndReceive(IAsyncResult ^ asyncResult, System::Net::IPEndPoint ^ % remoteEP);
public byte[] EndReceive (IAsyncResult asyncResult, ref System.Net.IPEndPoint remoteEP);
member this.EndReceive : IAsyncResult *  -> byte[]
Public Function EndReceive (asyncResult As IAsyncResult, ByRef remoteEP As IPEndPoint) As Byte()

參數

remoteEP
IPEndPoint IPEndPoint IPEndPoint IPEndPoint

指定的遠端端點。The specified remote endpoint.

傳回

Byte[]

如果成功,即為包含資料包資料的位元組陣列。If successful, an array of bytes that contains datagram data.

例外狀況

asyncResult 不會經由對 BeginReceive(AsyncCallback, Object) 方法的呼叫傳回。asyncResult was not returned by a call to the BeginReceive(AsyncCallback, Object) method.

嘗試存取基礎 Socket 時發生錯誤。An error occurred when attempting to access the underlying Socket.

範例

下列程式碼範例會BeginSend使用來完成伺服器回應的非同步接收。The following code example uses BeginSend to complete an asynchronous receive of 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);
    }
}

備註

這個方法會封鎖, 直到作業完成為止。This method blocks until the operation is complete.

若要同步執行此作業, 請Receive使用方法。To perform this operation synchronously, use the Receive method.

適用於