Lightweight C# server template.

Start Server

Method startServer(). Since we don't want the status messages cleared from the main text box, we use '.AppendText' and '\r\n' to add a new line. Uses an instance of '_httpListener' to set the prefix and start the listener. Then start a new listener thread.

Server Listener and Response

Create new instance of '*HttpListenerContext*': HttpListenerContext context = _httpListener.GetContext(); The response to incoming requests is a byte array, representing ASCII characters that form HTML markup to be displayed in the client browser: byte[] _responseArray = Encoding.UTF8.GetBytes("FireServer" + "FireServer is running"); The byte array is then written to HttpListenerContext for the output stream, and the response is terminated after sending: context.Response.OutputStream.Write(_responseArray, 0, _responseArray.Length);
context.Response.KeepAlive = false;
context.Response.Close();

Stop Server

Handled by 'stopServer()'. This attempts to terminate the httpListener thread with '_httpListener.Stop()'.

Restart Server

I'm working on a method that terminates the existing HttpListenerContext and creates a new instance of it.

References