Hi,
i found articles to that topic, but they dont solve my problem... I want to use UDP-Sockets for my XNA-Networkgame. And now i am trying to code a reliable Listenerthread, but there are some Problems.
If i use socket.Receive it will wait until a packet. This is fine for my Listenerthread. My thread has a while-loop like this:
while(Listen == true)
{
socket.Receive(...);
}
But if i swap the Listen-Flag to false (if i want to stop listening), it will stuck in the last .Receive().
Then i looked at the Methodes .BeginReceive(). It will call a methode if a packet arrived. But to receive the data i have to use .EndReceive() and that is the point i have a problem with. I want to still listen for packets and dont stop listening if a packet is arriving.
So i still use the blocking version with ".Receive()". I could force the listening thread to cancel by calling: Thread.abort(), but this is not good.
Currently I test if data is available:
while(Listen == true)
{
if(socket.Available > 0)
{
socket.Receive(...);
}
}
But i think this isnt the best way... If shortly after the if-clause a other thread is calling socket.Receive(..) it will stuck unintentional again. Is there no way to cancel the .Receive(..) methode? I tried to set a timeout, but if .Receive timesout, it will throw an exception...
I want a simple udp-listening-thread, i can stop gracefully. :-) In MSDN i didnt found a listener-example which is listening for more than one packet. How handle other programmer this?
Thank you for reading,
Greetings
user437899