加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

使用node-ipc和unix套接字在NodeJS和C之间进行通信

发布时间:2020-12-15 18:59:51 所属栏目:安全 来源:网络整理
导读:我想通过Unix套接字使用 node-ipc在NodeJS和C程序之间进行通信,根据该主页,这是最快的选择. (他们将在同一台机器上).该软件包声称它可以与C程序通信. (我必须进行健全检查). 问题是the examples不提供示例C代码,我接下来不知道我怎么让他们说话. 有人能指出
我想通过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”我想要的呢?)这对我来说没有任何意义,我错过了一些至关重要的东西.

我最终使用下面的代码完成了它.你可以免费获得它!

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,sizeof(addr));
  addr.sun_family = AF_UNIX;
  if (*socket_path == '') {
    *addr.sun_path = '';
    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: %.*sn",rc,buf);
    }
    if (rc == -1) {
      perror("read");
      exit(-1);
    }
    else if (rc == 0) {
      printf("EOFn");
      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套接字与Node在NodeJS之间进行通信,那么NodeJS实际上是带有a module that does that already.事实证明,这是node-ipc在引擎盖下使用的内容.因此,使用NodeJS的网络包可能更容易.
This previous question points out how to do IPC in NodeJS.将它与上面的C代码结合起来.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读