如何在网页和实际程序之间进行交互

时间:2021-04-07 22:52:56

What I have:

我拥有的:

  • A program (c++) on a server (linux)
  • 服务器上的程序(c ++)(linux)

  • The program can be executed with command line parameters
  • 可以使用命令行参数执行该程序

  • The program finishes after a while and produces continously output ("log.txt")
  • 程序在一段时间后结束并产生连续输出(“log.txt”)

What I want:

我想要的是:

My idea is to have some kind of user interaction with that program:

我的想法是与该程序进行某种用户交互:

  1. The user opens a website which shows some inputs (checkboxes, etc.)
  2. 用户打开一个显示一些输入的网站(复选框等)

  3. After submitting (POST) to the server the program should be started with chosen settings
  4. 提交(POST)到服务器后,应使用所选设置启动程序

  5. While running log.txt changes made by the program should be displayed to the user
  6. 运行log.txt时,应该向用户显示程序所做的更改

  7. When finished a message should occur (e.g. "Done")
  8. 完成后应发生一条消息(例如“完成”)

What would be the best approach? My current idea is to start a background process (php execute a background process ) and monitor the process id. However, I do not know how to dynamically monitor the process with php, how to show the user the log.txt output or how to tell the user that the program has finished, because these things are not static but dynamic.

什么是最好的方法?我目前的想法是启动后台进程(php执行后台进程)并监视进程ID。但是,我不知道如何用php动态监视进程,如何向用户显示log.txt输出或如何告诉用户程序已经完成,因为这些东西不是静态的而是动态的。

I am quite fine with C++, but my html and php skills are basic. Maybe I need a further technology for this?

我对C ++很好,但我的HTML和PHP技能是基本的。也许我需要一个进一步的技术呢?

1 个解决方案

#1


1  

<?php 

ob_implicit_flush(true);
ob_end_flush();

$cmd = "your_program -{$_POST[option1]} -{$_POST[option2]}";

$descriptorspec = array(
   0 => array("pipe", "r"),
   1 => array("pipe", "w"),
   2 => array("pipe", "w")
);
flush();
$process = proc_open($cmd, $descriptorspec, $pipes, realpath('./'), array());
echo "<pre>";
if (is_resource($process)) {
    while ($s = fgets($pipes[1])) {
        print $s;
        flush();
    }
}
echo "</pre>";

proc_close();

echo 'Proc finished!';

as answered here, but with a few modifications.

在这里回答,但稍作修改。

#1


1  

<?php 

ob_implicit_flush(true);
ob_end_flush();

$cmd = "your_program -{$_POST[option1]} -{$_POST[option2]}";

$descriptorspec = array(
   0 => array("pipe", "r"),
   1 => array("pipe", "w"),
   2 => array("pipe", "w")
);
flush();
$process = proc_open($cmd, $descriptorspec, $pipes, realpath('./'), array());
echo "<pre>";
if (is_resource($process)) {
    while ($s = fgets($pipes[1])) {
        print $s;
        flush();
    }
}
echo "</pre>";

proc_close();

echo 'Proc finished!';

as answered here, but with a few modifications.

在这里回答,但稍作修改。