Note that there are some explanatory texts on larger screens.

plurals
  1. POC#: UDP Listener Thread
    primarykey
    data
    text
    <p>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.</p> <p>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:</p> <pre><code>while(Listen == true) { socket.Receive(...); } </code></pre> <p>But if I swap the Listen-Flag to false (if I want to stop listening), it will stuck in the last .Receive().</p> <p>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 don't stop listening if a packet is arriving.</p> <p>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.</p> <p>Currently I test if data is available:</p> <pre><code>while(Listen == true) { if(socket.Available &gt; 0) { socket.Receive(...); } } </code></pre> <p>But I think this isn't 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(..) method? I tried to set a timeout, but if .Receive timesout, it will throw an exception...</p> <p>I want a simple udp-listening-thread, I can stop gracefully. :-) In MSDN I didn't find a listener-example which is listening for more than one packet. How handle other programmer this?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload