Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a web server that responds to every incoming request with a simple message

Tags:

c#

.net

webserver

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?

like image 982
ghostantanieh Avatar asked Jun 27 '10 17:06

ghostantanieh


People also ask

How can you handle incoming request message and return response messages with a web server?

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.

How do web servers handle multiple requests?

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.

How the web server does respond to the client request?

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.


2 Answers

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...

like image 197
Jon Skeet Avatar answered Oct 13 '22 11:10

Jon Skeet


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

like image 27
Iraklis Avatar answered Oct 13 '22 11:10

Iraklis