###状況
PHPからexeファイルを呼び出して、ブラウザ上でタイマーを作っています。
PHPにはあまり慣れていないのでサイトから引用しました。
イメージはサイトにアクセスしたら、0のタイマーが表示され、それからCのカウンターの値を受け取り続けタイマーの値を更新していくというものです。
ご指摘より:Cからの出力をリアルタイムにブラウザ経由で出力できるものを作りたいです。
###問題
Cの中でループさせているものを実行するとPHPの$return_valueに -1073741790 が返ってきます。
error-output.txtには何も出力されません。
そもそもstream_get_contents()を使うべきではないのかなとか思っています。
どうにかして状況で示したとおり、Cの処理内容を継続的に取得し続けることはできないでしょうか。
PHPだけでなくJavaScriptなどのクライアントサイドの処理でも問題ありません。
###ソースコード
php
1<?php 2 3$descriptorspec = array( 4 0 => array("pipe", "r"), 5 1 => array("pipe", "w"), 6 2 => array("file", "error-output.txt", "a") 7); 8 9$a_exe = '\fullpath\timer.exe'; 10 11$process = proc_open($a_exe, $descriptorspec, $pipes); 12 13if(is_resource($process)) { 14 15 echo stream_get_contents($pipes[1]); 16 fclose($pipes[1]); 17 18 $return_value = proc_close($process); 19 20 echo "error? : $return_value\n"; 21}
c
1#include <stdio.h> 2#include <stdlib.h> 3#include <time.h> 4 5int main() { 6 7 time_t t1, t2; 8 int during = 0, before; 9 10 t1 = time(NULL); 11 12 while(1) { 13 before = during; 14 t2 = time(NULL); 15 during = (int)(t2 - t1); 16 if(before != during) { 17 printf("%d!\n", during); 18 } 19 } 20 21 return EXIT_SUCCESS; 22 23}
回答2件
あなたの回答
tips
プレビュー