我想炫耀我的C++通过网站进行项目

发布于 2024-08-06 01:32:32 字数 372 浏览 2 评论 0 原文

问题是,它是 C++。我创建它们的方式使得它们始终通过终端/控制台窗口运行并等待用户输入,或者简单地获取示例输入并使用它运行。输出也始终输出到终端屏幕,有时输出到文件。我不太确定如何将所有这些内容与网站集成,同时保留源代码不变(如果可能的话)。我想我的目标是让我使用的任何网站都像终端窗口一样,接受用户输入,然后将其发送出去运行相关的 C++ 程序并返回输出(无论它是什么) ,所有这些都只需对源代码进行最少的修改。要么设置一个更自动化的页面,用户只需单击“Go”,程序就会使用示例输入运行。

当谈到网络时,我认为自己在 HTML、CSS、PHP 和 Web 领域处于中等水平。 MySQL 和 Javascript 初学者,因此如果可以使用这些语言来完成此任务,那就太棒了。如果没有,请不要害怕向我展示新的东西。

The problem is that, well, it's C++. The way I've created them makes it such that they've always been run via a terminal/console window and wait for user input or else simply take a sample input and run with that. The output has also always been to the terminal screen or sometimes to a file. I'm not quite sure how I could take all of that and integrate it with a website while leaving the source code as it is, if that's at all possible. I guess what I'm trying to aim for is to have whatever website I use behave like a terminal window that will accept user input and then send it off to run the C++ program in question and return with the output (whatever it may be), all with minimal modification to the source code. Either that or else set up a more automated kind of page where a user can just click 'Go' and the program will run using a sample input.

When it comes to web I consider myself intermediate with HTML, CSS, PHP & MySQL, and a beginner with Javascript, so if this can be accomplished using those languages, that would be fantastic. If not, don't be afraid to show me something new though.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

累赘 2024-08-13 01:32:32

引入网络的最简单的交互模型是一个应用程序,它预先获取输入并在 stdout 上生成输出。在这种情况下,正如未知发帖人提到的,您可以使用 CGI。但由于 CGI 的性质,只有在一页中从用户收集所有信息、发送到应用程序并在一页中返回结果时,这才有效(从最简单的意义上来说)。这是因为每次使用 CGI 调用页面都会产生一个新的独立进程来服务请求。 (现在还有其他更有效的解决方案,例如 FastCGI,它保留了一个进程池。 )如果您的应用程序是交互式的,因为它收集一些信息,呈现一些结果,打印一些选项,收集更多的用户输入,然后产生更多的结果,则需要进行调整。

这是关于 C++ 中最简单的 CGI 程序:

#include <iostream>
int main(int argc, char* argv[])
{
    std::cout << "Content-type: text/plain\n" << std::endl;
    std::cout << "Hello, CGI World!" << std::endl;
}

它所做的只是返回内容类型,后跟一个空行,然后是带有通常无聊问候语的实际内容。

要接受用户输入,您需要用 HTML 编写一个表单,而 POST 目标将是您的应用程序。它将以通常的 HTTP 风格传递一个包含请求参数的字符串:

foo.cgi?QTY=123&N=41&DESC=Simple+Junk

然后您需要解析查询字符串(通过 QUERY_STRING 环境变量传递给程序)以收集表单中要传递到您的应用程序的输入字段。请注意,解析参数字符串是大量安全漏洞的根源。找到一个用于 C++ 的 CGI 库(Google 搜索可以找到很多)来为您进行解析绝对是值得的。查询数据可以通过以下方式获取:

const char* data = getenv("QUERY_STRING");

因此,至少您需要更改应用程序以接受来自 name=value 对的查询字符串的输入。如果您不想,甚至不需要生成 HTML;只需将内容类型返回为 text/plain 即可。然后您可以稍后使用 HTML 对其进行改进(并相应地更改内容类型)。

还有其他更复杂的解决方案,包括整个 Web 框架,例如 Wt。但这将涉及对您的应用程序进行大量更改,您表示希望避免这种情况。

The easiest interaction model to bring to the web is an application that takes its input up front and produces its output on stdout. In this situation, as the unknown poster mentioned, you could use CGI. But due to the nature of CGI, this will only work (in the simplest sense) if all the information is collected from the user in one page, sent to the application and the results returned in one page. This is because each invocation of a page using CGI spawns a new indepdent process to serve the request. (There are other more efficient solutions now, such as FastCGI which keeps a pool of processes around.) If your application is interactive, in that it collects some information, presents some results, prints some options, collects some more user input, then produces more results, it will need to be adapted.

Here is about the simplest possible CGI program in C++:

#include <iostream>
int main(int argc, char* argv[])
{
    std::cout << "Content-type: text/plain\n" << std::endl;
    std::cout << "Hello, CGI World!" << std::endl;
}

All it does is return the content type followed by a blank line, then the actual content with the usual boring greeting.

To accept user input, you would write a form in HTML, and the POST target would be your application. It will be passed a string containing the parameters of the request, in the usual HTTP style:

foo.cgi?QTY=123&N=41&DESC=Simple+Junk

You would then need to parse the query string (which is passed to the program via the QUERY_STRING environment variable) to gather the input fields from the form to pass to your application. Beware, as parsing parameter strings is the source of a great number of security exploits. It would definitely be worthwhile finding a CGI library for C++ (a Google search reveals many) that does the parsing for you. The query data can be obtained with:

const char* data = getenv("QUERY_STRING");

So at a minimum, you would need to change your application to accept its input from a query string of name=value pairs. You don't even need to generate HTML if you don't want to; simply return the content type as text/plain to begin with. Then you can improve it later with HTML (and change the content type accordingly).

There are other more sophisticated solutions, including entire web frameworks such as Wt. But that would involve considerable changes to your apps, which you said you wished to avoid.

戏舞 2024-08-13 01:32:32

几乎偏离主题,但您可能想看看 Wt

Almost off-topic, but you might want to take a look at Wt.

じ违心 2024-08-13 01:32:32

你有没有考虑过使用cgi ...它是19世纪的技术,它可以让网络服务器执行用C/C++编写的程序来运行并生成输出

我对此不太了解...但我将它用于一些学校项目

have you considered using cgi ... its 19th century technology which lets webserver execute programs written in C/C++ to run and generate output

I do not know much about it ... but I used it for some school projects

绅士风度i 2024-08-13 01:32:32

通过截屏视频展示这一切。我使用 Camtasia Studio,但有很多这样的工具:http://en.wikipedia.org /wiki/Screencast

Camtasia 甚至会生成您需要上传到网络服务器的所有 HTML 和 Flash。买一个漂亮的 USB 麦克风,并写下你要说和展示的内容的脚本。

Show it all off with Screencasts. I use Camtasia Studio, but there are a ton of them out there: http://en.wikipedia.org/wiki/Screencast

Camtasia will even generate all of the HTML and Flash you need to upload to your web server. Buy a nice USB microphone, and write a script of what you're going to say and show.

我不会写诗 2024-08-13 01:32:32

炫耀你的项目的目的是什么?您想给您的朋友或雇主留下深刻印象吗?

通过 Web 界面模拟或移植 C++ 控制台应用程序似乎不太可行。
我想您可以在服务器端脚本和 C++ 二进制文件之间编写一个桥梁,将用户输入传递到您的应用程序,然后通过 Web 界面返回结果。请记住,这对您来说将是一项艰巨的任务。

Ruby 在其网站上有一个编译器,演示了这是可以完成的。

然而,网络上没有人会期望在网络浏览器中运行您的 C++ 应用程序。另外,我认为任何对运行 C++ 应用程序感兴趣的人都会完全放心地下载您制作的 C++ 二进制文件并运行它(除了安全风险),但当您想到这一点时,我们会下载应用程序并一直运行它们,同时信任来源。

我创建了一个作品集网站,其目的是让雇主看到我的作品。看一下,它会让您了解另一种做事的方式。

基本上我提供了用于下载的二进制文件、视频、屏幕截图和链接。如果用户没有时间(或没有合适的计算机)来运行我的项目,他们可以使用这些东西来快速查看我的工作。

祝你好运

What is the purpose of showing off your projects? Do you wish to impress your friends or employers?

It doesn't seem feasible to emulate or port your C++ console apps through a web interface.
I suppose you could write a bridge between a server side script and your C++ binary which passes the user input through to your app, then returns the result through the web interface. Bear in mind this would be a huge task for you to undertake.

Ruby have a compiler on their website which demonstrates this can be done.

However no one on the web would expect to run your C++ apps in a web browser. Also I think that anyone who is interested in running a C++ app would be totally comfortable with downloading a C++ binary that you made and running it (apart from the security risk) but when you think about it we download apps and run them all the time, whilst trusting the source.

I have a portfolio website which I created for the purpose of letting employers see my work. Take a look, it will give you an idea of another way you can do things.

Basically I provide the binaries for download, videos, screenshots and links. Things that the user can use to see my work quickly if they don't have time (or an appropriate computer) to run my projects on.

Good luck

安静被遗忘 2024-08-13 01:32:32

我对此没有任何经验(除了听 BART 上的一个人谈论如何用 C 实现他的服务器端代码),但您可能会考虑看看 SWIG (http://www.swig.org/)。它允许您包装 C++,以便您在使用 PHP 等语言时可以访问 C++ 代码。

I have no experience with this (other than hearing a guy on BART talk about implementing his server-side code all in C), but you might consider taking a look at SWIG (http://www.swig.org/). It allows you to wrap C++ so that you can access C++ code when using languages such as PHP.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文