title: receive from multiple UDP endpoints
body: <p>my question applies to this piece of code:</p>
  
  <pre><code>using DSemulator;
  using System;
  using System.Collections.Generic;
  using System.ComponentModel;
  using System.Diagnostics;
  using System.Linq;
  using System.Net;
  using System.Net.Sockets;
  using System.Text;
  
  namespace Stuff
  {
  public class interfaceServer
  {
          int portNumber = 8000;
          IPEndPoint ipep, iclient;
          UdpClient clientUdp;
          BackgroundWorker worker = new BackgroundWorker();
  
          List&lt;UdpClient&gt; clients = new List&lt;UdpClient&gt;();
  
          public interfaceServer()
          {
                  ipep = new IPEndPoint(IPAdress.Any, portNumber);
                  iclient = new IPEndPoint(IPAddress.Any, 0);
  
                  clientUdp = new UdpClient();
                  clientUdp.ExclusiveAddressUse = false;
                  clientUdp.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
  
                  clientUdp.Client.Bind(ipep);
  
                  worker.DoWork += delegate { worker_DoWork(clientUdp, iclient); };
                  worker.RunWorkerAsync();
          }
  
          void worker_DoWork(UdpClient udpClient, IPEndPoint remoteEndpoint)
          {
                  bool read = true;
                  while (read)
                  {
                          Byte[] data_rec = new Byte[128];
                          try
                          {
                                  data_rec = udpClient.Receive(ref remoteEndpoint);
                          }
                          catch (SocketException)
                          {
                                  IPEndPoint endpoint = new IPEndPoint(IPAddress.Any, 0);
                                  UdpClient tempClient = new UdpClient();
                                  tempClient.ExclusiveAddressUse = false;
                                  tempClient.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
                                  tempClient.Client.Bind(endpoint);
  
                                  worker.DoWork += delegate { worker_DoWork(tempClient, endpoint); };
                          }
  
                          //do interesting stuffzz
                          //the UdpClient and remoteEndpoint need to be passed to objects of another class.
                          //these objects handle sending data back
                  }
          }
  }
  }
  </code></pre>
  
  <p>Due to company-secrets, I can not show the whole code, but this is where my problem lies.</p>
  
  <p>I have a program, who simple said must receive Udp packages. The packages can be send from any IP-adress and from any portnumber. There is also no way to say how much connections there will be. All these connections need to be processed at the same time (same time, for us as humans), I cannot wait until one socket has received something and then move on to another receive, that's why I implemented the threads.
  Messages will be received on a single port, 8000 in this case.</p>
  
  <p>In the code I have made, I made a try-catch statement for receiving messages. The 'try' part will fail if there is a message received from another IP/port, so in the catch I make another socket and thread for this other IP/port.</p>
  
  <p>I think this is a good solution, or at least the start of one, but I'm still wondering over some stuff.</p>
  
  <ol>
  <li>How do I start the added thread? I can not wait for all threads to finish, then call worker.RunWorkerAsync(); again. If I do that, lot of information can be lost, so that's no option.</li>
  <li>Because the 'try' will fail in all threads, there will be lots of threads made for a single socket. How do I solve that?</li>
  </ol>
  
  <p>Also, if I am totally thinking in the wrong direction, I'd be glad to hear.</p>
  
  <p>Thanks in advance!</p>