views:

236

answers:

7

Is there a relatively easy way to display the output of a C++ program on a webpage? And I don't mean manually, in other words, you see it on a webpage as it runs not as in I make a code tag and write it in myself.

EDIT: Just so everybody can get this clear I am going to post this up here. I am NOT trying to make a webpage in C++. Please excuse me if this sounds spiteful or anything but I am getting a lot of answers relating to that.

A: 

I am not sure this is what you are looking for but you may want CGI You may want to look at this SO question, C++ may not be the best language for what you want to do.

based off the questions you posted Writing a web app like what you want is no simple task. What I would recommend is use some other library (this is one i found with a quick google) to get a web console on your server and give the user it is running under execute deny permissions on every folder except the folder you have your app installed.

This is still is a risky method if you don't set up the security correctly but it is the easiest solution without digging around too much on existing libraries to just have the application interactive.

EDIT -- The "Best" solution is learn AJAX and have your program post its own pages with it but like I said, it will not be easy.

Scott Chamberlain
I'm not trying to write a webpage in C++. I'm trying to display output of a C++ program.
thyrgle
Do you want the output to be live or just every time they refresh they get the current buffer?
Scott Chamberlain
I want the output to be live. Essentially, what I am aiming on doing is someone writes some code in C++ and it displays the output of the program that person wrote on the website so users can interact with just as if it were in Terminal or Command Prompt.
thyrgle
Will it be expecting any user input?
Scott Chamberlain
Yes. It will be expecting input.
thyrgle
Well I got a lot of time, wasn't expecting it to be easy.
thyrgle
The problem with HTTP is that it is a request/response model. You will need to wrap your main program into a web request handler. You can stream the current output out, but standard input is going to have to be handled via AJAX calls back to your program.
Yann Ramin
This is an absolutely horrible solution. Using PHP (or another server side language) is fifty times more secure than direct access and a few hundred times easier.
Oli
Guys, I am not trying to write a webpage in C++.
thyrgle
@thyrgle no comment in this answer suggests you are.
Oli
@Oli: Ok, so you are saying then that it is a horrible solution for doing what I am trying to do and that I should use a server side language like PHP to display C++ output? If so I have misunderstood you.
thyrgle
+2  A: 

You could write a CGI app in C++, or you could use an existing web server language to execute the command and send the output to the client.

Ignacio Vazquez-Abrams
+3  A: 

Step one, get yourself a server-side language. Be that PHP, ASP, Python, Ruby, whatever. Get it set up so you can serve it.

Step two, find your language's exec equivalent. Practically all of them have them. It'll let you run a command as if it were from the command line, usually with arguments and capture the output. Here's PHP's:

http://php.net/manual/en/function.exec.php

Of course, if you're passing user-input as arguments, sanitise!


I've just seen that you accepted Scott's answer. I usually wouldn't chase up a SO thread so persistently but I fear you're about to make a mistake that you'll come to regret down the line. Giving direct access to your program and its own built-in server is a terrible idea for two reasons:

  1. You waste a day implementing this built-in server and then getting it to persist and testing it

  2. More importantly, you've just opened up another attack vector into your server. When it comes to security, keep it simple.

You're far better having your C++ app running behind another (mature) server side language as all the work is done for you and it can filter the input to keep things safe.

Oli
A: 

It sounds like you want something like a telnet session embedded in a webpage. A quick google turns up many Java telnet apps, though I'm not qualified to evaluate which would be most ideal to embed in html.

You would set up the login script on the host machine to run your c++ app and the user would interact with it through the shell window. Note though that this will only work for pure command line apps. If you want to use a GUI app in this way, then you should look into remote desktop software or VNC.

Alan
+2  A: 

You want to use Witty.

Wt (pronounced 'witty') is a C++ library for developing interactive web applications.

The API is widget-centric and similar to desktop GUI APIs. To the developer, it offers complete abstraction of any web-specific implementation details, including event handling, graphics support, graceful degradation (or progressive enhancement), and pretty URLs.

Unlike many page-based frameworks, Wt was designed for creating stateful applications that are at the same time highly interactive (leveraging techinques such as AJAX to their fullest) and accessible (supporting plain HTML browsers), using automatic graceful degradation or progressive enhancement.

The library comes with an application server that acts as a stand-alone web server or integrates through FastCGI with other web servers.

scomar
A: 

Hi,

It may be worth looking into Adobe's "Alchemy" project on Adobe Labs

This may help you with what you're trying to achieve.

:)

Sam Nicholson
A: 

Are you looking for something like what codepad.org does? I believe they explain how they did it here.

rlbond
codepad doesn't allow for user input.
Troubadour