UdpClient.EndReceive(IAsyncResult, IPEndPoint) 메서드

정의

보류 중인 비동기 수신을 종료합니다.

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 * IPEndPoint -> byte[]
Public Function EndReceive (asyncResult As IAsyncResult, ByRef remoteEP As IPEndPoint) As Byte()

매개 변수

asyncResult
IAsyncResult

호출 IAsyncResult 에서 반환되는 개체입니다 BeginReceive(AsyncCallback, Object).

remoteEP
IPEndPoint

지정된 원격 엔드포인트입니다.

반품

Byte[]

성공하면 데이터그램 데이터가 포함된 바이트 배열입니다.

예외

asyncResultnull입니다.

asyncResult 가 메서드 호출에 BeginReceive(AsyncCallback, Object) 의해 반환되지 않았습니다.

EndReceive(IAsyncResult, IPEndPoint) 는 이전에 비동기 읽기에 대해 호출되었습니다.

기본 Socket에 액세스하려고 할 때 오류가 발생했습니다.

기본이 Socket 닫혔습니다.

예제

다음 코드 예제에서는 서버 응답의 비동기 수신을 완료하는 데 사용합니다 BeginSend .

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);
    }
}

설명

이 메서드는 작업이 완료될 때까지 차단합니다.

이 작업을 동기적으로 수행하려면 메서드를 Receive 사용합니다.

적용 대상