From b3b0fdebc84127de20a961b11b984377f344467d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Sim=C3=B5es?= Date: Tue, 14 Oct 2025 00:53:18 +0100 Subject: [PATCH 1/2] Add constructor with IP address - Add parameter to WebServer constructor and rework constructors accordingly. - Update README to add note for this new parameter. --- README.md | 3 ++- nanoFramework.WebServer/WebServer.cs | 34 +++++++++++++++++++++++++--- 2 files changed, 33 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index fe2d810..d34fbe1 100644 --- a/README.md +++ b/README.md @@ -36,7 +36,8 @@ This library provides a lightweight, multi-threaded HTTP/HTTPS WebServer for .NE Using the Web Server is very straight forward and supports event based calls. ```csharp -// You need to be connected to a wifi or ethernet connection with a proper IP Address +// You need to be connected to a Wi-Fi or ethernet connection with a proper IP Address +// Optionally you can pass a parameter with the IP address for the server to bind to using (WebServer server = new WebServer(80, HttpProtocol.Http)) { diff --git a/nanoFramework.WebServer/WebServer.cs b/nanoFramework.WebServer/WebServer.cs index f552e22..f3e97ce 100644 --- a/nanoFramework.WebServer/WebServer.cs +++ b/nanoFramework.WebServer/WebServer.cs @@ -263,7 +263,11 @@ public static UrlParameter[] ExtractRouteParameters(string route, string rawUrl, /// /// Port number to listen on. /// version to use with web server. - public WebServer(int port, HttpProtocol protocol) : this(port, protocol, null) + /// IP address to bind to. + public WebServer( + int port, + HttpProtocol protocol, + IPAddress address) : this(port, protocol, address, null) { } /// @@ -272,14 +276,38 @@ public WebServer(int port, HttpProtocol protocol) : this(port, protocol, null) /// Port number to listen on. /// version to use with web server. /// Controllers to use with this web server. - public WebServer(int port, HttpProtocol protocol, Type[] controllers) + public WebServer( + int port, + HttpProtocol protocol, + Type[] controllers) : this(port, protocol, null, controllers) + { } + + /// + /// Instantiates a new web server. + /// + /// Port number to listen on. + /// version to use with web server. + /// IP address to bind to. If , will bind to default address. + /// Controllers to use with this web server. + public WebServer( + int port, + HttpProtocol protocol, + IPAddress address, + Type[] controllers) { _callbackRoutes = new ArrayList(); + RegisterControllers(controllers); + Protocol = protocol; Port = port; + string prefix = Protocol == HttpProtocol.Http ? "http" : "https"; - _listener = new HttpListener(prefix, port); + + _listener = new HttpListener( + prefix, + port, + address); } private void RegisterControllers(Type[] controllers) From 90604daae50733d7297ca6bc3dfee307050bff7d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Sim=C3=B5es?= Date: Tue, 14 Oct 2025 01:53:18 +0100 Subject: [PATCH 2/2] Fixes from review --- nanoFramework.WebServer/WebServer.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nanoFramework.WebServer/WebServer.cs b/nanoFramework.WebServer/WebServer.cs index f3e97ce..c75abb4 100644 --- a/nanoFramework.WebServer/WebServer.cs +++ b/nanoFramework.WebServer/WebServer.cs @@ -263,7 +263,7 @@ public static UrlParameter[] ExtractRouteParameters(string route, string rawUrl, /// /// Port number to listen on. /// version to use with web server. - /// IP address to bind to. + /// IP address to bind to. If null, the server will bind to the default network interface. public WebServer( int port, HttpProtocol protocol,