Method HttpServerSession.Listen
Overload
Simple HTTP 1.1 server.
public static void Listen<TSession>(int port = 4455, string ip = null) where TSession : HttpServerSession, new()
Parameters
port (int)
TCP port. |
ip (string)
A local IPv4 or IPv6 address. If |
Exceptions
Exception
Exceptions of System.Net.Sockets.TcpListener functions. Unlikely. |
Type Parameters
TSession |
Remarks
Runs all the time and listens for new TCP client connections. For each connected client starts new thread, creates new object of your HttpServerSession-based type, and calls HttpServerSession.Run, which calls HttpServerSession.MessageReceived. Supports keep-alive. Multiple sessions can run simultaneously.
Uses System.Net.Sockets.TcpListener, not System.Net.HttpListener, therefore don't need administrator privileges, netsh
and opening ports in firewall. Just the standard firewall dialog first time.
The HTTP server is accessible from local network computers. Usually not accessible from the internet. To make accessible from the internet, you can use ngrok or similar software. This server does not support https (secure connections), but ngrok makes internet connections secure.
If ip is null
or an IPv6 address, supports IPv6 and IPv4 connections.
Examples
HTTP server.
HttpServerSession.Listen<MyHttpSession>();
class MyHttpSession : HttpServerSession {
//protected override void Run() {
// print.it($"Session started. Client IP: {ClientIP}");
// base.Run();
// print.it("Session ended");
//}
protected override void MessageReceived(HSMessage m, HSResponse r) {
//Auth((u, p) => p == "password206474");
print.it(m.Method, m.TargetPath, m.UrlParameters, m.Headers, m.ContentText);
string response = "RESPONSE";
r.SetContent(response);
//or
//r.Content = response.ToUTF8();
//r.Headers.Add("Content-Type", "text/plain; charset=utf-8");
}
}
HTTP client.
var res = internet.http.Get("http://127.0.0.1:4455/file.txt?a=1&b=2"/*, auth: "user:pw"*/).Text(); //from same computer
//var res = internet.http.Get("http://ComputerName:4455/file.txt?a=1&b=2"/*, auth: "user:pw"*/).Text(); //from local network
//var res = internet.http.Get("https://1111-11-111-11-111.ngrok-free.app/file.txt?a=1&b=2"/*, auth: "user:pw"*/).Text(); //from internet through ngrok
print.it(res);