【问题标题】:Communicating between NodeJS and C using node-ipc and unix sockets使用 node-ipc 和 unix 套接字在 NodeJS 和 C 之间进行通信
【发布时间】:2017-02-12 00:11:52
【问题描述】:

我想通过 Unix 套接字使用 node-ipc 在 NodeJS 和 C 程序之间进行通信,根据该主页,这是最快的选择。 (他们将在同一台机器上)。该软件包声称它可以与 C 程序通信。 (我必须进行完整性检查)。

问题是the examples 不要给出示例 C 代码,我几乎不知道如何让他们说话。

谁能给我一个 C 代码示例来匹配那些客户端/服务器示例?例如,how would I adapt this tutorial on working with unix pipes in C(假设我没有完全偏离轨道?!也许我想要的是“domain sockets”?)这对我来说没有任何意义,我错过了一些重要的东西。

【问题讨论】:

  • 您是否尝试双向通信程序?这是一个商业项目吗?你愿意为帮助付费吗?
  • 是的,双向。支付帮助并不是一个真正的选择,我只需要朝着正确的方向轻推。我本来打算使用 ZeroMQ,但它似乎完全是矫枉过正
  • 如果是在 POSIX 系统上,其实这是一个非常简单的任务。可悲的是,我不能免费帮助你,也不能在这里回答,因为我认为你需要的不仅仅是简单的帮助。 Unix 域套接字是要走的路,我相信如果你用谷歌搜索它,你会发现很多包含 c 代码的例子。而且它其实是一个很简单的东西,就像一个普通的socket,只是地址和类型不同。
  • C 在这里没有意义。您只需要一种可以进行系统调用的语言。如果您在网络上讲话,那么您使用哪种语言并不重要。
  • 我想索取有关此的更多信息,您是否能够/愿意分享您的项目以便我了解更多信息?

标签: c node.js sockets ipc node-ipc


【解决方案1】:

我最终使用下面的代码让它工作。你可以免费拥有它!

server.c

#include <stdio.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <stdlib.h>

char *socket_path = "/tmp/icp-test";

int main(int argc, char *argv[]) {
  struct sockaddr_un addr;
  char buf[100];
  int fd,cl,rc;

  if (argc > 1) socket_path=argv[1];

  if ( (fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
    perror("socket error");
    exit(-1);
  }

  memset(&addr, 0, sizeof(addr));
  addr.sun_family = AF_UNIX;
  if (*socket_path == '\0') {
    *addr.sun_path = '\0';
    strncpy(addr.sun_path+1, socket_path+1, sizeof(addr.sun_path)-2);
  } else {
    strncpy(addr.sun_path, socket_path, sizeof(addr.sun_path)-1);
    unlink(socket_path);
  }

  if (bind(fd, (struct sockaddr*)&addr, sizeof(addr)) == -1) {
    perror("bind error");
    exit(-1);
  }

  if (listen(fd, 5) == -1) {
    perror("listen error");
    exit(-1);
  }

  while (1) {
    if ( (cl = accept(fd, NULL, NULL)) == -1) {
      perror("accept error");
      continue;
    }

    while ( (rc=read(cl,buf,sizeof(buf))) > 0) {
      printf("read %u bytes: %.*s\n", rc, rc, buf);
    }
    if (rc == -1) {
      perror("read");
      exit(-1);
    }
    else if (rc == 0) {
      printf("EOF\n");
      close(cl);
    }
  }
  return 0;
}

client.js:

 var ipc=require('node-ipc');

var socketId = 'icp-test';
ipc.config.id   = 'hello';
ipc.config.socketRoot = '/tmp/';
ipc.config.appspace = '';

ipc.config.retry= 1500;
ipc.connectTo(
  socketId,
  function(){
    ipc.of[socketId].on(
      'connect',
      function(){
        console.log("Connected!!");
        ipc.log('## connected to world ##'.rainbow, ipc.config.delay);
        ipc.of[socketId].emit(
          'message',  //any event or message type your server listens for
          'hello'
        )
      }
    );
    ipc.of[socketId].on(
      'disconnect',
      function(){
        console.log("Disconnected!!");
        ipc.log('disconnected from world'.notice);
      }
    );
    ipc.of[socketId].on(
      'message',  //any event or message type your server listens for
      function(data){
        console.log("Got a message!!");
        ipc.log('got a message from world : '.debug, data);
      }
    );
  }
);

顺便说一句,我意识到如果你只想通过 unix 套接字与 C 进行 NodeJS 之间的通信,NodeJS 实际上带有a module that does that already。事实证明,这就是 node-ipc 在幕后使用的。因此,改用 NodeJS 的 net 包可能更容易。 This previous question points out how to do IPC in NodeJS. 只需将其与上述 C 代码结合即可。

【讨论】:

  • #include &lt;string.h&gt; 压制一些警告
  • @Jodes - 这真的很有帮助!您是如何实现双向通信的?如何将数据从 c 服务器传递到客户端?另外,我的目标是在客户端上使用 express 以便它接收请求并在某些时候将一些工作传递给 c 服务器。你知道请求是命名空间还是我需要线程?
  • @Jodes 我问了一个问题来澄清反向传递数据:stackoverflow.com/questions/52755754/…
【解决方案2】:

我认为您应该寻找 C unix 域套接字,而不是管道。管道不一样。在 Google 上寻找一些带有 unix 域套接字的 C 代码示例,尝试一些,让它工作,然后回到 node-ipc 的东西。

我去掉了 node-ipc 和 node.js 标签,因为如果你想先弄清楚 C 端,Node 就无关紧要了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-04-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-20
    • 2021-06-15
    • 2013-01-27
    • 1970-01-01
    • 2012-01-26
    相关资源
    最近更新 更多