2011-02-01 44 views
1

我覺得這很奇怪。試圖使用UdpClient同步接收廣播消息,但它不會返回,雖然廣播已完成。只有在隨後的廣播中,我才收到消息。我認爲框架內的某處存在一個錯誤?UdpClient接收直到第二次廣播才返回

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 

感謝, 肯尼

+0

哪裏AsyncCall.AsyncState價值從何而來? – RobinG 2011-02-01 12:07:05

+0

它來自同一個UdpClient對象。 clientListener.BeginReceive(新的AsyncCallback(RxBcastCallback),clientListener); //這是在回調中作爲AsyncCall.AsyncState傳遞 – Kenny 2011-02-01 12:26:22

回答

1

你的回調端點大概應該是:

UdpClient udpListener = (UdpClient)AsyncCall.AsyncState; 
IPEndPoint e = (IPEndPoint)((UdpState)(AsyncCall.AsyncState)).e; 
byte[] inBuffer= udpListener.EndReceive(AsyncCall, ref e);