To create an HTTP server on a specific port in C#, you can use the HttpListener
class from the System.Net
namespace. Here is an example:
main.cs1611 chars54 lines
In this example, we first define a port number (8080) and create an HttpListener
object. We then add the URL prefix for the server (http://localhost:8080/) and start listening for incoming requests.
We use an infinite loop to continuously wait for incoming requests and process them by writing a "Hello, World!" response to the client.
Note: this example does not handle exceptions or errors. In a production server, you would need to handle errors gracefully and close the HttpListener
object before exiting the program.
gistlibby LogSnag