HttpServer class
private TcpListener Listener { get; set; } public void Listen() { Listener = new TcpListener(IPAddress.Any, 2000); Listener.Start(); while (true) { var client = Listener.AcceptTcpClient(); ThreadPool.QueueUserWorkItem((state) => Handle(client)); //Handle each request on it's own thread. } } private void Handle(TcpClient client) { using (var io = client.GetStream()) using (var reader = new StreamReader(io)) using (var writer = new StreamWriter(io)) { //Request string[] line = reader.ReadLine()?.Split(); //Do not read to end! It would hang. string method = line[0]; //GET, POST, ... string url = line[1]; //The requested URL. //Response string content = "<html><body>EGS</body></html>"; writer.WriteLine("HTTP/1.1 200 OK"); writer.WriteLine("Content-Type: text/html; charset=UTF-8"); writer.WriteLine($"Content-Length: {content.Length}"); writer.WriteLine("Connection: close"); writer.WriteLine(); writer.WriteLine(content); } client.Close(); }
Start the server
Server = new HttpServer(); new Thread(new ThreadStart(Server.Listen)).Start(); //On a separate thread, so the UI doesn't hang.