UdpClient.BeginReceive(AsyncCallback, Object) Metodo

Definizione

Riceve un datagramma da un host remoto in modalità asincrona.

public:
 IAsyncResult ^ BeginReceive(AsyncCallback ^ requestCallback, System::Object ^ state);
public IAsyncResult BeginReceive (AsyncCallback? requestCallback, 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

Parametri

requestCallback
AsyncCallback

Delegato AsyncCallback cui fa riferimento il metodo da richiamare al completamento dell'operazione.

state
Object

Oggetto definito dall'utente contenente informazioni sull'operazione di ricezione. Questo oggetto viene passato al delegato requestCallback al completamento dell'operazione.

Restituisce

Oggetto IAsyncResult che fa riferimento alla ricezione asincrona.

Esempio

Nell'esempio di codice seguente viene BeginReceive usato per ricevere in modo asincrono una risposta del server.

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

Commenti

L'operazione asincrona BeginReceive deve essere completata chiamando il EndReceive metodo . In genere, il metodo viene richiamato dal requestCallback delegato.

Questo metodo non viene bloccato fino al completamento dell'operazione. Per bloccare fino al completamento dell'operazione, usare il Receive metodo .

Per informazioni dettagliate sull'uso del modello di programmazione asincrona, vedere Chiamata asincrona di metodi sincroni.

Si applica a