views:

107

answers:

2

For my newest windows-application I want to create an interface that is web-based using the web-browser control, and I want to make Ajax work on it. Since all the content will be loaded by the web-browser control itself I want to create a web-server that would work on a localhost port and serve my content to the web-browser when it accesses a special url, example:

http://localhost:3454

How can I create a web-server that windows will route requests to? I just need to know about the right API/WinInet commands to start receiving the requests.

+1  A: 

All you have to do is open a TCP socket, bind to port 3454 and listen for connections. Then you need to service each incoming connection by reading the stream, parsing the HTTP headers and content (if applicable).

Judge Maygarden
+3  A: 

Fortunate for you, .NET comes with a pretty decent Web Server built into the framework.

Look at the HttpListener class.

It supports both synchronous and asynchronous modes:

The synchronous model is appropriate if your application should block while waiting for a client request and if you want to process only one request at a time. Using the synchronous model, call the GetContext method, which waits for a client to send a request. The method returns an HttpListenerContext object to you for processing when one occurs.

In the more complex asynchronous model, your application does not block while waiting for requests and each request is processed in its own execution thread. Use the BeginGetContext method to specify an application-defined method to be called for each incoming request. Within that method, call the EndGetContext method to obtain the request, process it, and respond.

This guy is great because it uses the native web server that is built into modern versions of Windows (XP SP2+ and Server 2003+).

Frank Krueger