1

I think this is really weird. Tried to use UdpClient to synchronously receive broadcast msg, but it does not return although the broadcast has completed. Only on subsequent broadcast did I get the message. I think there's a bug somewhere within the framework?

clientListener.BeginReceive(new AsyncCallback(RxBcastCallback), clientListener);
//this is passed as AsyncCall.AsyncState in the callback

.....

UdpClient udpListener = (UdpClient)AsyncCall.AsyncState;        

IPEndPoint remoteEndPt = new IPEndPoint(IPAddress.Any, 0);

byte[] inBuffer = udpListener.Receive(ref remoteEndPt); 
//does not return until subsequent broadcast

thanks, Kenny

RobinG
  • 196
  • 6
Kenny
  • 13
  • 2
  • Where does AsyncCall.AsyncState value come from? – RobinG Feb 01 '11 at 12:07
  • It's from the same UdpClient object. clientListener.BeginReceive(new AsyncCallback(RxBcastCallback), clientListener);//this is passed as AsyncCall.AsyncState in the callback – Kenny Feb 01 '11 at 12:26
  • Well, there's your answer. Your callback is receiving the first message asynchronously, and then your sync Receive call is receiving the second - in all likelyhood. – RobinG Feb 01 '11 at 12:35

1 Answers1

1

Your callback endpoint should probably be:

UdpClient udpListener = (UdpClient)AsyncCall.AsyncState;
IPEndPoint e = (IPEndPoint)((UdpState)(AsyncCall.AsyncState)).e;
byte[] inBuffer= udpListener.EndReceive(AsyncCall, ref e);
RobinG
  • 196
  • 6
  • Yes Robin, you're right. Rather than calling Receive synchronously, I should call EndReceive. The API is really baffling (and it's easy to fall into a hole) Thanks - seems to work now. – Kenny Feb 01 '11 at 13:09