gpt4 book ai didi

c++ - Node.js:使用带有 exe 程序的 SPAWN 分割标准输出

转载 作者:行者123 更新时间:2023-11-28 04:51:06 24 4
gpt4 key购买 nike

我写了一些 C++ 程序,它正在等待 103 个数据字符串和 9 个字符串输出的响应。我想从 Node 应用程序发送数据并从输出接收数据。不幸的是,我发现我在控制台中显示的输出数据不是来自同一个读取周期。所以问题是: Node 应用程序中的标准输入和标准输出是否有任何我可以刷新的缓冲区或其他东西?

这是我的 C++ 程序:

#include <iostream>
#include <string>
#include <vector>

using namespace std;

int main()
{
cout << "Program is running";
vector <string> tab(103);
string nazwa;
int index = 0;
do {
for (int i = 0; i < 103; i++) {
getline(cin, nazwa);
if (!nazwa.empty() && nazwa[nazwa.size() - 1] == '\n')
nazwa.erase(nazwa.size() - 1);
if (!nazwa.empty() && nazwa[nazwa.size() - 1] == '\r')
nazwa.erase(nazwa.size() - 1);
tab[i] = nazwa;
}
for (int i = 0; i < 9; i++) {
cout << index << ": " << i+1 << endl;
}
index++;
} while (true);
return 0;
}

它只是等待 103 个带有来自 Node 的数据的字符串并返回 9 个字符串。这是我的 Node 程序:

var prg = null;
var str = "";
var counter = 0;
var spawn = require('child_process').spawn;
var fun = function(){
console.log("fun() start");
prg = spawn('test.exe');
};
fun();
setInterval(function() {
//console.log(".");
prg.stdin.write('0.5\n');
}, 10);

prg.stdout.on('data', function(data) {
str = data.toString(), lines = str.split(/(\r&\n)/g);
for (var i=0;i<lines.length; i++) {
console.log('node counter: '+counter+", passed data: "+lines[i]);
}
counter++;
});

Node 控制台的输出证明输出被分割:

fun() start
node counter: 0, passed data: Program is running
node counter: 1, passed data: 0: 1
0: 2
0: 3
0: 4
0: 5
0: 6
0: 7

node counter: 2, passed data: 0: 8
0: 9

node counter: 3, passed data: 1: 1
1: 2
1: 3
1: 4
1: 5
1: 6
1: 7
1: 8
1: 9

更新:我找到了 3 个解决方案。我必须通过 IPC(进程间通信)传递数据以避免这些标准输入/输出缓冲区。

可能的方式:

我使用了很棒的 git 项目 JSON for Modern C++传递易于使用的数据容器。

我创建了 JSON 结构并将其放入文件中。现在我可以读取这个文件并将它发送到管道。管道的主要问题是很难控制来自管道的数据字节。但是,如果您将 JSON 用于现代 C++ 插件,您就会知道何时拥有所有数据。因为在每次管道读取中我都不知道这次我将发送多少字节,所以我创建了更大的缓冲区并且在从管道读取所有数据后我只是剪切它们并再次使用 JSON 解析器。读取数据后(C++ 端)我正在准备此数据以将其发送回 Node 。

Node 代码:

var prg = null;
var fs = require('fs');
//parse JSON from file
var obj = JSON.parse(fs.readFileSync('data.json', 'utf8'));

var net = require('net');
var path = require('path');
var counter = 0;
//create pipe
var server = net.createServer(function(req) {
setInterval(function() {
var strobj = JSON.stringify(obj);
req.write(strobj); // send data to pipe every second
}, 1000);
rehttps://stackoverflow.com/questionsq.on('data', function(c) {
var size = c.length;
console.log(counter+': bytesize of JSON: ' + size);
var reciv = JSON.parse(c);
counter++;
console.log("Display JSON:\n %j ",reciv);
});
});
server.listen('\\\\.\\pipe\\JointsPipe');

// Spawning EXE program
var spawn = require('child_process').spawn;
var prg = spawn('test.exe');

prg.on('close', function(){
console.log("Pipe Closed");
});

C++ Program

希望对大家有帮助。

最佳答案

我假设 node.js child_process.spawn 创建了一个子进程,它共享它的父进程 stdin、stdout 和 stderr 流。由于 parent 和 child 都在写入这些流,因此您将获得“混合”输出。您可以尝试将其中的每一个重定向到不同的文件并获得不分割的输出。或者您可以尝试使用一些共享资源在父进程和子进程之间进行通信(例如,一些套接字或命名管道)

关于c++ - Node.js:使用带有 exe 程序的 SPAWN 分割标准输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48230331/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com