I'd like to create a web server that responds to every incoming request with a simple "Hello" message in C#.
How do I do that?
One solid approach is to use Microsoft Message Queue (MSMQ) to submit messages into a queue, and have another application pick up the incoming request to process. The result is then returned in a response message that the Web application can poll for.
The server opens a socket that 'listens' at port 80 and 'accepts' new connections from that socket. Each new connection is represented by a new socket whose local port is also port 80, but whose remote IP:port is as per the client who connected. So they don't get mixed up.
HTTP Basics The client (usually a browser) opens a connection to the server and sends a request. The server processes the request, generates a response, and closes the connection if it finds a Connection: Close header.
How much of it do you need to do from scratch? It's pretty simple to do this if you're allowed to use HttpListener
.
Otherwise, you might want to look at TcpListener
- accept a socket connection, read data from it, and write a response. Admittedly it's somewhat easier if you can answer every request with "Hello" as you don't need to really parse it...
You can read an article on CodeProject
http://www.codeproject.com/KB/IP/mywebserver.aspx
There is also a project on codeplex
http://webserver.codeplex.com/
You will need Sockets, Multithreading and the RFC http://www.faqs.org/rfcs/rfc2616.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With