2015-05-02 99 views
0

我有一個C++程序讀取文件並一次打印一行(每行一行)。管道標準輸出到瀏覽器

我需要從PHP腳本內執行的代碼,並將輸出傳送回瀏覽器。到目前爲止,我已經嘗試execpassthru,但在這兩種情況下,程序執行後,整個輸出在瀏覽器上「轉儲」。如何獲得PHP將輸出流傳回瀏覽器。

這是我迄今爲止寫的代碼:
sender.php:發送要執行的請求。

<?php 

/* 
* Purpose of this program: 
* To display a stream of text from another C++-based program. 
* Steps: 
* 1. Button click starts execution of the C++ program. 
* 2. C++ program reads a file line-by-line and prints the output. 
*/ 

?> 

<html> 

<head> 
    <script type="text/javascript" src="js/jquery-1.11.2.min.js"></script> 
    <script type="text/javascript"> 
     $(document).ready(function() { 
      $("#startDisplaying").click(function() { 
       console.log("Starting to display."); 
       /* 
       //initialize event source. 
       if(!!window.EventSource) { 
        console.log("Event source is available."); 
        var evtSource = new EventSource("receiver.php"); 

        evtSource.addEventListener('message', function(e) { 
         console.log(e.data); 
        }, false); 

        evtSource.addEventListener('open', function(e) { 
         console.log("Connection opened."); 
        }, false); 

        evtSource.addEventListener('error', function(e) { 
         console.log("Error seen."); 
         if(e.readyState == EventSource.CLOSED) { 
          console.log("Connection closed."); 
         } 
        }, false); 
       } else { 
        console.error("Event source not available."); 
       } 
       */ 
       $.ajax({ 
        type: 'POST', 
        url: 'receiver.php', 
        success: function(data) { 
         console.log("Data obtained on success: " + data); 
         $("#displayText").text(data); 
        }, 
        error: function(xhr, textStatus, errorThrown) { 
         console.error("Error seen: " + textStatus + " and error = " + errorThrown); 
        } 
       }); 
      }); 
     }); 
    </script> 
</head> 

<body> 
<textarea id="displayText"></textarea><br/> 
<button id="startDisplaying">Start Displaying</button> 
</body> 

</html> 

的receiver.php,執行該程序:

<?php 

/* This file receives the request sent by sender.php and processes it. 
* Steps: 
* 1. Start executing the timedFileReader executable. 
* 2. Print the output to the textDisplay textarea. 
*/ 

header('Content-Type: text/event-stream'); 
header('Cache-control: no-cache'); 

function sendMsg($id, $msg) { 
    echo "id: $id".PHP_EOL; 
    echo "data: $msg".PHP_EOL; 
    echo PHP_EOL; 
    ob_flush(); 
    flush(); 
} 

$serverTime = time(); 

$cmd = "\"timedFileReader.exe\""; 
//sendMsg($serverTime, 'server time: '.exec($cmd, time())); 
passthru($cmd); 

/* 
$cmd = "\"timedFileReader.exe\""; 
exec($cmd. " 2>&1 ", $output); 
print_r($output); 
*/ 
?> 

C++程序:

#include<fstream> 
#include<string> 
#include<unistd.h> 
#include<cstdlib> 
#include<iostream> 
using namespace std; 

int main(int argc, char *argv[]) { 

    ifstream ifile; 
    string line; 

    ifile.open("file.txt"); 
    if(!ifile) { 
    cout << "Could not open file for reading." << endl; 
    exit(0); 
    } 

    while(getline(ifile, line)) { 
    cout << line << endl; 
    //usleep(5000000); //sleep for 1000 microsecs. 
    sleep(1); 
    } 

    return 0; 
} 

這是模型執行的甚至有可能在PHP?
任何幫助是最受歡迎的。

+0

除非你計劃動態編譯程序,然後運行它,語言並不重要 - 你只是運行一個二進制文件。 – Shomz

回答

相關問題