Search code examples
c#arraysfunctionwebserver

C# Pass multiple arguments to function and array


I'm trying to start my "webserver" on all local IP's. So I have to send a string to WebServer ws = new WebServer(SendResponse, IPs.GetIPs("program"));, and a Array to static string[] uris.

Now it only receives one IP, tried some other ways like sending a string to WebServer ws = new WebServer(SendResponse, IPs.GetIPs("program")); but that also didn't work with multiple IP's.

It needs to return a string for program and a array for webserver.

The string should be something like this: "http://192.168.0.107:1337/", "http://192.168.56.1:1337/" for program.

How would I send more then 1 argument to a function and to a array. I know this code doesn't work but I'm desperate right now to get this working.

IPs.cs:

public static string GetIPs(string args)
    {
        string[] combinedString = { };
        List<string> IPAdressenLijst = new List<string>();
        IPAddress[] ips = Dns.GetHostAddresses(Dns.GetHostName());

        foreach (IPAddress ip in ips)
        {
            if (ip.AddressFamily == AddressFamily.InterNetwork)
            {
                if (args == "program")
                {
                    IPAdressenLijst.Add("http://" + ip + ":1337/");
                    combinedString = IPAdressenLijst.ToArray();
                }
                else if (args == "webserver")
                {
                    IPAdressenLijst.Add("http://" + ip + ":1337/start/");
                    IPAdressenLijst.Add("http://" + ip + ":1337/stop/");
                    combinedString = IPAdressenLijst.ToArray();
                }
            }
        }
        return combinedString[0];
    }

Program.cs:

static void Main(string[] args)
    {
        Console.WriteLine(DateTime.Now + " Press any key to exit.");
        //WebServer ws = new WebServer(SendResponse, "http://192.168.0.107:1337/", "http://localhost:1337/");
        WebServer ws = new WebServer(SendResponse, IPs.GetIPs("program"));
        ws.Run();
        Console.ReadKey();
        ws.Stop();
    }

public static string SendResponse(HttpListenerRequest request)
{
    return string.Format("TEST");
}

WebServer.cs:

public class WebServer
{
    //static string[] uris =
    //{
    //    "http://192.168.0.107:1337/start/",
    //    "http://192.168.0.107:1337/stop/"
    //};
    static string[] uris =
    {
        IPs.GetIPs("webserver")
    };
}

Solution

  • To start a TcpListener (for your webserver) you can use the IPAddress.Any address like below:

    var server = new TcpListener(IPAddress.Any, port);
    

    Now, to your code: The GetIPs function has bunch of issues. While you loop, you keep overwriting combinedString, every time. To top it, you actually just return the first item from combinedString. From the context of the question, I gather that you want to return all IP addresses and not just one

    This is how that function should be

    public static List<string> GetIPs(string args) {
        var ipAdressenLijst = new List<string>();
        IPAddress[] ips = Dns.GetHostAddresses(Dns.GetHostName());
    
        foreach (IPAddress ip in ips) {
            if (ip.AddressFamily == AddressFamily.InterNetwork) {
                if (args == "program") {
                    ipAdressenLijst.Add("http://" + ip + ":1337/");
                } else if (args == "webserver") {
                    ipAdressenLijst.Add("http://" + ip + ":1337/start/");
                    ipAdressenLijst.Add("http://" + ip + ":1337/stop/");
                }
            }
        }
        return ipAdressenLijst;
    }
    

    Your Webserver.cs code should look like this to handle the incoming array of URIs.

    public class WebServer {
        string[] Uris;
        public WebServer(Func<HttpListenerRequest, string> sendResponse, IEnumerable<string> ipAdressenLijst)
        {
            Uris = ipAdressenLijst.ToArray();
        }
    }
    

    In Main function in Program.cs, your call should look like

    WebServer ws = new WebServer(SendResponse, IPs.GetIPs("webserver"));
    

    There is a lot of guess work on my side, as not all information is available in your question.. Let me know if that makes sense and works for you