Web Application Programming using C++

Pages: 12
May 1, 2012 at 9:33am
As the topic says, I wanted to know if we could program a web application in C++. I mean, you can do stuffs in php. Google Appengine allows Python and Java. Can we use C/C++ ?
May 1, 2012 at 9:48am
Google also has NaCl, which executes C and C++ in a browser: http://developers.google.com/native-client/
Last edited on May 1, 2012 at 9:48am
May 1, 2012 at 11:37am
NaCl is for client stuff only. There are some web toolkits for C/C++, but IMHO this is not the right tool for the job. Writing web stuff in C++ is like writing hard-real-time applications in Java.
May 1, 2012 at 11:49am
Web Applications? Java, C#, ASP, PHP are the things that come to mind. Like rapidcoder said, C++ is the wrong tool for the job. C++ is for software development and not web applications. You wouldn't use assembly to make a web page.
May 1, 2012 at 4:38pm
Pravesh Koirala wrote:
Can we use C/C++ ?

I agree that C/C++ is not the right tool to use but to answer your question. The answer is YES! I tried to write a simple form processing program before.

[edit] add links
http://www.guyrutenberg.com/2007/08/10/introduction-to-c-cgi/
http://www.yolinux.com/TUTORIALS/LinuxTutorialC++CGI.html
Last edited on May 1, 2012 at 4:46pm
May 1, 2012 at 5:07pm
I've written a simple library for CGI in C++. You can get it here: http://github.com/ChrisSwinchatt/CGIPP
At the moment, all it does is parse & simplify POST and GET requests, so it's quite minimalist in that respect. Ideally I'll be adding more features to it later.

It's quite easy to use. Here is an example of parsing GET requests:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <cgipp.hpp>

int main(int argc, char* argv[])
{
	cgipp::CGI cgi("text/html", cgipp::HttpMethod::GET);
	std::cout << "<html>\n"
		  << "\t<head>\n"
		  << "\t\t<title>" << cgi.GET("title") << "</title>\n"
		  << "\t</head>\n"
		  << "\t<body>\n"
		  << "\t\t" << cgi.GET("body") << "\n"
		  << "\t</body>\n"
		  << "</html>\n";
	return 0;
}


This one parses POST data and prints the results.
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <cgipp.hpp>
#include <iostream>
#include <string>

int main(int argc, char* argv[])
{
	cgipp::CGI cgi;
	for (cgipp::HttpDataIterator it = cgi.Request.Data.begin();
				it < cgi.Request.Data.end(); ++it) {
		std::cout << "Variable " << it->first << " = " << it->second << '\n';
	}
	return 0;
}
Last edited on May 1, 2012 at 5:11pm
May 2, 2012 at 8:36am
CGI:
1. terribly slow
2. terribly unproductive way of writing apps
3. error-prone, easy to make vulnerabilities
4. so nineties*

:D

* However, the language that was the recommended way to write CGIs in nineties was Perl, not C or C++.
Last edited on May 2, 2012 at 8:38am
May 2, 2012 at 11:09am
its okay.. I'm learning python for web programming. Its just that I wanted to know whether or not C++ can be used for web programming. Thanks guys!
May 2, 2012 at 4:15pm
@rapidcoder,
You're right, and I haven't used that library myself, I just felt like writing it.

PHP is the way to go for server-side scripts.
May 3, 2012 at 3:28pm

PHP is the way to go for server-side scripts


Only for simple ones. It doesn't scale well, both in terms of project size and load size, especially if used together with MySQL.
May 3, 2012 at 4:08pm
What do you recommend instead?
May 3, 2012 at 6:14pm
I think most larger applications use Java (well, of course that's not very much 'scripting').

I heard RoR is good, though I only did the first few parts of the Rails for Zombies series. Also heard Seaside is good if you're actually writing a web application. (Though I don't know how hard it is to find a webhost that supports seaside).
Last edited on May 3, 2012 at 6:15pm
May 3, 2012 at 7:55pm
rapidcoder wrote:
It doesn't scale well, both in terms of project size and load size, especially if used together with MySQL.


I was under the impression facebook's backend was PHP and SQL. But I heard they had some special compiler that compiles PHP to C code.
May 4, 2012 at 12:30pm

I was under the impression facebook's backend was PHP and SQL


And who told you Facebook scales well? ;) They are just very desperate. Actually they had a *lot* of problems with scalability. They are fighting them with memcached, clever architecture and lots of duct tape, but well, other big data companies achieve scalability probably with much less effort using proper databases that are designed to scale out of the box (e.g. google).
Last edited on May 4, 2012 at 12:31pm
May 4, 2012 at 1:20pm
I use my C++ program exclusively from the web browser via CGI and apache web server. I used to use wxWidgets (before that FOX GUI and before that native WINAPI), but actually CGI + chrome browser is far superior to any GUI toolkit I tried. One more thing: as Apache automatically caches your .exe in RAM, in fact boot time for your program is much better.

Alternatively, you can implement your own socket connections and circumvent using Apache.

Here is my calculator project computing the 100th fibonacci number:

http://cartan.math.umb.edu/vpf/cgi-bin/calculator?textType=Calculator&textDim=1&textInput=fib%7B%7D0%3A%3D1%3B%0D%0Afib%7B%7D1%3A%3D0%3B%0D%0Afib%7B%7D%7B%7Bn%7D%7D%3A%3Dfib%7B%7D%28n-1%29%2Bfib%7B%7D%28n-2%29%3B%0D%0Afib%7B%7D100&buttonGo=Go

Last edited on May 4, 2012 at 1:24pm
May 6, 2012 at 5:02pm
Yes, you can!

This includes using new technologies like HTML5 and Ajax.

See:
http://www.webtoolkit.eu/wt // Wt, C++ Web Toolkit

http://www.codeproject.com/Articles/336018/Building-C-Applications-with-HTML5 // Qt, WebKit

As an alternative, you can also use Emscripten to "compile C and C++ code into JavaScript and run that on the web": http://emscripten.org/

HTH!
May 7, 2012 at 3:42am
Hey can Java do all web-based stuff and C++ do everything else? (I know they can theoretically do both but I mean with ease)
May 7, 2012 at 7:37am
What "everything else" do you mean? Java is perfect on the server side, so if I used Java there, why would I need to use also C++? And on the client side, you can use JS+HTML5 or Flash, or if you need better performance - Java or Silverlight.
Last edited on May 7, 2012 at 7:37am
May 7, 2012 at 7:58am
but I mean with ease


I wouldn't go so far as to say "with ease". Java does have built-in support for networking though, so it's definitely easier.
May 7, 2012 at 1:41pm

As an alternative, you can also use Emscripten to "compile C and C++ code into JavaScript and run that on the web


Yeah, if you don't mind making everything 100x - 200x slower, you can do it that way. Hand optimised JS is still order of magnitude slower than Java/C++, and automatically generated JS is another order of magnitude slower than hand optimised JS.
Pages: 12