views:

123

answers:

3

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?

+3  A: 

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

Jon Skeet
I guess I am allowed to use the HttpListener but how? would u please help me?
ghostantanieh
@ghostantanieh: Did you look at the example in the referenced documentation? It's not ideal, but it's at least a starting point...
Jon Skeet
+1  A: 

You need to implement some Socket Programming. use System.Net.Sockets.TcpListener class to listen incomming requests.

// Use port number 80 for incomming HTTP requests.
TcpListener listener = new TcpListener(80);

// This method return a socket to handle incomming client's request...
listener.AcceptSocket();

Also you need to know how handle multithreading programming. because of this you need to create a thread and read just incomming requests and synchronously save client's thread and socket in a collection and process data!

I don't want to disappointed you but you need a little bit of parsing strings using Teory of Languages and Machines!

Jalal Amini
you know?? I don't really know the socket programming ?? what is it??? also I don't know where exactly in my code shall I put this bit?
ghostantanieh
+1  A: 

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

Iraklis
do you know how can I return a hello message with every request? is it just with write console?
ghostantanieh
For which link?
Iraklis