我在 Windows 上使用 PHP 脚本与国际象棋引擎进行通信。我建立连接如下:
$descriptorspec = array(
0 => array("pipe", "r"), // stdin is a pipe that the child will read from
1 => array("pipe", "w"), // stdout is a pipe that the child will write to
2 => array("file","./error.log","a")
);
$resource = proc_open("stockfish.exe", $descriptorspec, $pipes, null, array());
我可以像这样向引擎发送命令:
fwrite($pipes[0], "uci" . PHP_EOL);
我读了这样的引擎输出:
stream_get_contents($pipes[1]);
问题是我无法读取引擎输出,直到我像这样关闭标准输入管道:
fclose($pipes[0]);
这意味着每当我想与引擎交互时,我必须不断地打开和关闭连接(使用 proc_open)。
如何始终保持连接打开?