public void ReceiveMessages()
{
// Receive a message and write it to the console.
if (u == null)
{
e = new IPEndPoint(IPAddress.Any, 8002);
u = new UdpClient(e);
}
UdpState s = new UdpState();
s.e = e;
s.u = u;
MessageBox.Show("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
int TimeCount = 0;
while (!messageReceived)
{
if (TimeCount++ < 10)
{
Thread.Sleep(200);
}
else
{
MessageBox.Show("超时");
break;
}
}
}
public void ReceiveCallback(IAsyncResult ar)
{
//connectDone.Set();
UdpClient u = (UdpClient)((UdpState)(ar.AsyncState)).u;
IPEndPoint e = (IPEndPoint)((UdpState)(ar.AsyncState)).e;