views:

88

answers:

3

Hi,

Is it possible to write a web application with C which acts both as the application and web server.

I mean, typically, web applications are written in PHP which is invoked by Apache. If you want to write an extremely fast and efficient small application can you write an executable to work independent of Apache and PHP interpreter?

Many thanks, Majid

Edit: I want to write a simple game and the game is multi-player and should run as a server, client parts can be html forms within a browser. Doy you know of any simple open source game I can use as a skeleton? I want something which does not require a separate web-server or database-server

+2  A: 

Sure it is possible -- if you can write an OS with C, you can write a web server and application. It'll just be a lot more work.

Here's an example C web server with only 200 lines of code.

Kaleb Brasee
Do you know of any examples of such an application? Perhaps a simple (Open source) game?
Majid
Posted an example link. It only servers static pages however, so you might need to google for something a bit more robust.
Kaleb Brasee
Thanks Kaleb. Will try it.
Majid
For .Net, it might work to use the new IIS Express webserver: http://weblogs.asp.net/scottgu/archive/2010/06/28/introducing-iis-express.aspx
David
+1  A: 

Sure you could write one yourself. The HTTP protocol is vast but you don't have to be a fully compliant web server to run your own application.

The C application would have to listen to requests on whatever port you decide to run the server at (default 80). There are various request methods in HTTP, but at a minimum you would have to implement GET, or maybe even POST but that aspect you can control since its your web application.

Here's an example of a basic request that you C program should parse:

 GET /index.html HTTP/1.1
 Host: www.example.com

And return a response with the requested content and response headers, such as:

 HTTP/1.1 200 OK
 Date: Mon, 23 May 2005 22:38:34 GMT
 Server: Apache/1.3.3.7 (Unix)  (Red-Hat/Linux)
 Last-Modified: Wed, 08 Jan 2003 23:11:55 GMT
 Etag: "3f80f-1b6-3e1cb03b"
 Accept-Ranges: bytes
 Content-Length: 438
 Connection: close
 Content-Type: text/html; charset=UTF-8

I may have already mentioned this but the HTTP protocol is really vast. Browsers with varying capabilities could access the site, and you would have to respond accordingly. Having said that, since it's all under your control, you could start small with perhaps a small subset of the requests/headers you'd like to handle, single-threaded, one request at a time, etc. and then build it out from there.

Like Kaleb said if C can be used in OS's and spaceships, then we can definitely write a web and application server with it :)

Anurag
A: 

I just hiphop-php which could be used for this purpose. Here is what they say about it:

HipHop for PHP transforms PHP source code into highly optimized C++. It was developed by Facebook and was released as open source in early 2010.

It could be accessed here

Majid