使用node-ipc和unix套接字在NodeJS和C之间进行通信 [英] Communicating between NodeJS and C using node-ipc and unix sockets

查看:762
本文介绍了使用node-ipc和unix套接字在NodeJS和C之间进行通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to communicate between NodeJS and a C program using node-ipc, over a Unix socket, which according to that homepage is the fastest option. (They will be on the same machine). That package claims it could communicate with a C program. (I had to sanity check).

问题是示例没有给出示例C代码,我几乎不知道如何让他们交谈.

The problem is the examples don't give example C code, and I have next to no idea how I'd get them to talk.

有人能指出我一个C代码示例以匹配那些客户机/服务器示例吗?例如,我将如何修改本教程中有关使用Unix管道的信息C (假设我还没有完全脱离轨道?!也许是"域套接字",而是我想要的?)这一切对我来说都没有意义,我缺少了一些关键的东西.

Can anyone point me to an example of C code to match those client/server examples? For example, how would I adapt this tutorial on working with unix pipes in C (Assuming I'm not completely off track?! Maybe it's "domain sockets" that I want instead?) None of this is making any sense to me, I'm missing something crucial.

推荐答案

我最终将其与下面的代码一起使用.您可以免费获得它!

I got it to work in the end with the code below. You can have it for free!

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:

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

在旁注中,我已经意识到,如果您只是想通过带有C的unix套接字在NodeJS之间进行通信,则NodeJS实际上附带了此先前的问题指出了如何在NodeJS中执行IPC.上面的C代码.

On a side note, I've realised that if you just want to do communicate between NodeJS via unix sockets with C, NodeJS actually comes with a module that does that already. Which as it turns out, is what node-ipc uses under the hood. So it might be easier just to use NodeJS's net package instead. This previous question points out how to do IPC in NodeJS. Just combine that with the above C code.

这篇关于使用node-ipc和unix套接字在NodeJS和C之间进行通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆