C# Pass multiple arguments to function and array -
i'm trying start "webserver" on local ip's. have send string webserver ws = new webserver(sendresponse, ips.getips("program"));
, , array static string[] uris
.
now receives 1 ip, tried other ways sending string webserver ws = new webserver(sendresponse, ips.getips("program"));
didn't work multiple ip's.
it needs return string
program
, array webserver
.
the string should this: "http://192.168.0.107:1337/", "http://192.168.56.1:1337/" program
.
how send more 1 argument function , array. know code doesn't work i'm desperate right 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 key 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") }; }
to start tcplistener
(for webserver) can use ipaddress.any
address below:
var server = new tcplistener(ipaddress.any, port);
now, code: getips
function has bunch of issues. while loop, keep overwriting combinedstring
, every time. top it, return first item combinedstring
. context of question, gather want return ip addresses , not one
this how 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 handle 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, call should
webserver ws = new webserver(sendresponse, ips.getips("webserver"));
there lot of guess work on side, not information available in question.. let me know if makes sense , works you
Comments
Post a Comment