我有一個C代碼,在無限循環內等待輸入併產生輸出。IPC:使用簡單C代碼的節點JS服務器通信
#include<stdio.h>
void flush() {
int c;
while ((c = getchar()) != '\n' && c != EOF);
}
int main() {
/*
* Load many files here
*/
double d = 0;
char input[1024];
while (1) {
d = d + 0.1;
scanf("%[^\n]", input);
printf("%lf\n",d);
fflush(stdout);
flush();
}
}
我需要另一個節點JS服務,它將在某個端口上偵聽併發送輸出作爲響應。 我有這樣的代碼編寫
var http = require('http');
var spawn = require('child_process').spawn;
var child = spawn('./dummyNLU.out');
child.stdin.setEncoding('utf-8');
child.stdin.cork();
var buffer = new Buffer(100);
var app = http.createServer(function(req,res){
var string = buffer.write("HelloWorld!"); //this is for testing purpose
child.stdin.write(buffer);
child.stdin.end();
child.stdout.on('data', function(data) {
res.setHeader('Content-Type', 'text/plain');
res.end(data);
});
});
app.listen(3001);
這段代碼我似乎並不像是同所有工作。
節點JS服務器錯誤和網絡響應終止包括283線的響應,而不是1
任何人都可以請幫我?也歡迎其他一些解決此問題的方法(從C可執行代碼輸出回覆Web請求)。 預先感謝您。
的第一個請求會關閉子的標準輸入。你如何期待第二次請求被提供? –