gpt4 book ai didi

node.js - Node js 子进程 stdin stdout exe 通信

转载 作者:太空宇宙 更新时间:2023-11-03 22:54:25 25 4
gpt4 key购买 nike

我正在尝试与node.js 中的windows EXE 进行通信。我对 Node.js 还很陌生

使用这个示例作为起点,但我无法让它工作。
http://juristr.com/blog/2014/03/integrating-node-with-csharp/

可执行文件似乎未启动,因为当我运行它时 .connected 为 false。我不知道可执行文件在哪里/如何启动。

EXE代码(从命令行运行它工作正常,返回输入的任何内容)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LibraryExe
{
class Program
{
static void Main(string[] args)
{
string line;
do
{
line = Console.ReadLine();
try
{
Console.WriteLine("Recieved - " + line);
if (line == "quit")
break;
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
} while (line != null);
}
}
}

Node.js代码(似乎没有启动exe,connected = false)

    console.log('Hello world');

var spawn = require('child_process').spawn;
var posProc = spawn('C:/path/LibraryExe.exe', []);

posProc.stdout.once('data', function (data) {
// write it back on the response object
console.log('stdOut');
writeToResponse('we got back ' + data);
});

posProc.on('exit', function (code) {
console.log('Closed');
writeToResponse('Library closed');
});

posProc.stdout.on('data', function (data) {
console.log('stdout: ' + data);
});

if (posProc.connected)
console.log('connected');
else
console.log('not connected');

posProc.stdin.setEncoding = 'utf-8';
posProc.stdin.write('Hello');
posProc.stdin.write('quit');
console.log('ending');

最佳答案

您的 C# 代码正在尝试读取,但您没有将任何 CRLF 对写入 stdin 流。试试这个:

posProc.stdin.write('Hello\r\n');
posProc.stdin.write('quit\r\n');

关于node.js - Node js 子进程 stdin stdout exe 通信,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27554707/

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