public static void ReceiveCallback(IAsyncResult ar) { UdpClient u = (UdpClient)((UdpState)(ar.AsyncState)).u; IPEndPoint e = (IPEndPoint)((UdpState)(ar.AsyncState)).e;
public static void ReceiveMessages() { // Receive a message and write it to the console. IPEndPoint e = new IPEndPoint(IPAddress.Any, 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); } }