【问题标题】:node js child spawn stdin stdout exe communicationnode js child spawn stdin stdout exe 通信
【发布时间】:2014-12-18 20:01:51
【问题描述】:

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

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

看起来可执行文件没有启动,因为当我运行它时 .connected 为假。 我看不到可执行文件的启动位置/方式。

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');

【问题讨论】:

    标签: node.js stdin child-process


    【解决方案1】:

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

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

    【讨论】:

    • 这样可以,但是怎么连接=假。我什至检查了“posProc.stdout.once()”内部的连接,它说那里没有连接。
    • 不要费心查看connected 和其他未记录的子进程对象的属性。它们的实现细节源于子进程对象某种基于套接字的事实(因此有connected 属性)。
    猜你喜欢
    • 1970-01-01
    • 2021-05-26
    • 2014-02-14
    • 1970-01-01
    • 2011-06-22
    • 2017-02-01
    • 2012-05-20
    • 1970-01-01
    • 2013-05-22
    相关资源
    最近更新 更多