Q:

C# UDP listener on multiple ports fetched from text box

I tried searching everywhere for this question with no avail.
What I am trying to accomplish is that I enter a list of ports into a text box.
then I create a udpClient array with these ports start listening to them.
static class communicator
    {
        // Setting Variables
        static UdpClient[] UDPreceiver;
        static TcpListener[] TCPreceiver;
        static IPAddress ipAddress = Dns.GetHostEntry("localhost").AddressList[0];
        static bool listening = false;

        // Listener function
        public static void UDPstartlistening(int port)
        {
            // Startlistening
            listening = true;
            while (listening)
            {
                try
                {
                    UDPreceiver[port] = new UdpClient(port); // udp server
                    if (UDPreceiver[port].Available > 0) // Only read if we have some data queued in buffer
                    {
                        //IPEndPoint object will allow us to read datagrams sent from any tracker.
                        IPEndPoint RemoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0);
                        // Blocks untill data is received
                        Byte[] receiveBytes = UDPreceiver[port].Receive(ref RemoteIpEndPoint);
                        string returnData = ByteArrayToString(receiveBytes);
                        // Uses the IPEndPoint object to determine who sent us anything
                        Program.form1.addlog("Received: " + returnData.ToString() + " - from " + RemoteIpEndPoint.Address.ToString() + " on port: " + RemoteIpEndPoint.Port.ToString());
                        // Forward this message to the website
                        Task.Run(() => forwardToWebsite(returnData.ToString(), RemoteIpEndPoint.Address.ToString(), RemoteIpEndPoint.Port, "udp", port));
                    }
                    Thread.Sleep(10);
                }
                catch (Exception e)
                {
                   MessageBox.Show("Source : " + e.Source + "\r\n" + "Message : " + e.Message, "Error");
                }
            }
        }

It gives me "Object reference not set to an instance of an object." on the line that says"UDPreceiver[port].Available".
Am I doing this the right way ?

A:

Try this, it contains some bug fixes:

put your receivers in a list instead of an un-assigned array
client should be declared outside the read-loop
you are aware of the fact that this will block the calling thread?
  => use a new Thread(() => { ... }); to run the receiving part in another thread  

Code below:
static class communicator
{
    // Setting Variables
    static List<UdpClient> UDPreceivers = new List<UdpClient>();
    //static List<TcpListener> TCPreceivers = new List<TcpListener>();
    static IPAddress ipAddress = Dns.GetHostEntry("localhost").AddressList[0];
    static bool listening = false;

    // Listener function
    public static void UDPstartlistening(int port)
    {
        UdpClient UDPreceiver = new UdpClient(port); // udp server
        UDPreceivers.Add(UDPreceiver);

        // Startlistening
        listening = true;
        while (listening)
        {
            try
            {
                if (UDPreceiver.Available > 0) // Only read if we have some data queued in buffer
                {
                    //IPEndPoint object will allow us to read datagrams sent from any tracker.
                    IPEndPoint RemoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0);
                    // Blocks untill data is received
                    Byte[] receiveBytes = UDPreceiver.Receive(ref RemoteIpEndPoint);
                    string returnData = ByteArrayToString(receiveBytes);
                    // Uses the IPEndPoint object to determine who sent us anything
                    Program.form1.addlog("Received: " + returnData.ToString() + " - from " + RemoteIpEndPoint.Address.ToString() + " on port: " + RemoteIpEndPoint.Port.ToString());
                    // Forward this message to the website
                    Task.Run(() => forwardToWebsite(returnData.ToString(), RemoteIpEndPoint.Address.ToString(), RemoteIpEndPoint.Port, "udp", port));
                }
                Thread.Sleep(10);
            }
            catch (Exception e)
            {
                MessageBox.Show("Source : " + e.Source + "\r\n" + "Message : " + e.Message, "Error");
            }
        }
    }
}

