是否有可能通过单击html页面上的按钮在我的Web服务器上运行c ++程序


Is there possibility to run a c++ program on my webserver through button click on the html page?

在过去的几个小时里,我做了很多研究,只找到了Windows服务器的结果。

我的问题是我在嵌入式计算机上有一个c ++程序,该程序需要在用户希望时执行。这就是为什么我设置了一个网络服务器来处理这个问题。所以我想创建一个带有一个名为"启动程序"的按钮的 html 页面(已经完成),然后执行我的 c++ 程序。

我已经尝试了 Javascript child_process它不知何故不起作用,并且还尝试了 php system()调用,这不方便,因为它不仅对按钮单击做出反应,而且对页面刷新也有反应。

有没有一个聪明的方法可以做到这一点?谢谢!

如果你不想刷新页面,你可以使用jquery对按钮进行编程,以进行ajax调用。您可以在 html 索引页面中添加以下内容:

<script type="text/javascript" src="https://code.jquery.com/jquery-2.2.1.js"></script>
<script>
    <!--
    $(document).ready(function() {
        $('#buttonid').on('click', function(e) {
            e.preventDefault(); // prevent page from reloading.
            $.get('/callmyprogram.php');
        });
    });
    -->
</script>

同样在您的 html 上,按钮的 ID 必须与上面 JS 代码上的选择器匹配。选择器#buttonid意味着按钮的 ID buttonid 。因此,按钮的 html 如下所示:

<input type="submit" id="buttonid" value="Run C++ program"/>

然后callmyprogram.php调用你的 c++ 程序:

<?php
exec("/usr/local/bin/myprogram", $output, $ret);
echo "output: $output" . PHP_EOL;
echo "ret: $ret" . PHP_EOL;
相关文章: