Node.js 如何刷新nodejs子进程stdin.write
我需要在服务器端为客户端运行一个“C”程序。此程序可以是交互式的。Node.js 如何刷新nodejs子进程stdin.write,node.js,Node.js,我需要在服务器端为客户端运行一个“C”程序。此程序可以是交互式的。 我正在使用Node.js子进程类。但我发现了一个问题:因为我需要保持程序的交互,所以客户端和node.js服务器之间会来回交换消息 1.程序显示:输入x的值: 将此消息发送到客户端并获取一些值作为输入 2.当Nodejs服务器从客户端接收到输入时,它会执行child_process.stdin.write 但问题是,在我标记流的结尾之前,程序不会执行。有什么办法可以解决这个问题? 比如在用户可用时将值刷新到程序中 更新: tes
我正在使用Node.js子进程类。但我发现了一个问题:因为我需要保持程序的交互,所以客户端和node.js服务器之间会来回交换消息 1.程序显示:输入x的值:
将此消息发送到客户端并获取一些值作为输入
2.当Nodejs服务器从客户端接收到输入时,它会执行child_process.stdin.write 但问题是,在我标记流的结尾之前,程序不会执行。有什么办法可以解决这个问题?
比如在用户可用时将值刷新到程序中 更新: test1.c
#包括
int main() {
int x, y;
printf("Enter x : ");
fflush(stdout);
scanf("%d", &x);
printf("Enter y : ");
fflush(stdout);
scanf("%d", &y);
printf("Value entered y is %d\n", y);
printf("Value entered x is %d", x);
}
编译上述代码以生成可执行文件a.out
var spawn = require('child_process').spawn;
var count = 0;
var exec = spawn('./a.out');
exec.on('error', function (err){
console.log("Error" + err );
});
exec.stdout.on('data', function(data){
console.log("in stdout.on.data : " + data.toString());
if (count == 0) {
exec.stdin.write('44\n');
count++;
}
else if (count == 1) {
exec.stdin.write('54');
count++;
}
if (count == 2) {
exec.stdin.end();
}
});
exec.stderr.on('data', function(data) {
console.log("in stderr.on.data : " + data);
});
exec.on('close', function (code) {
if (code != 0) {
console.log("Program ended with a error code : " + code);
}
});
在上面的node.js代码中,如果我注释exec.stdin.end()代码>它不工作,并等待流关闭。我怎样才能解决这个问题?因为我希望客户能够持续互动,所以我很难预测何时结束流 您忘记“按回车键”
您还可以查找一些关于的信息。您可以在问题中包含您的源代码吗?我找到了解决方案。因为exec.stdin.write('54')没有回车符;程序等待扫描。解决方案是在文字中添加“\n”。我已经在评论部分回答了这个问题:)
exec.stdin.write('54' + "\n");
OR
exec.stdin.write("54\n");