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

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

问题描述

以下是这个问题的继续:

This question follows on from the following:

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

关于公认的解决方案( https://stackoverflow.com/a/39848936/1834057 ),我想知道是否有人能够确切说明如何将数据从 C发送到Node.js .该解决方案演示了如何将数据从 Node.js发送到C ,但不能反向发送.我有一个需要双向通讯的应用程序,所以缺少的组件对我来说至关重要.

In regards to the accepted solution (https://stackoverflow.com/a/39848936/1834057), I was wondering if someone might be able to clarify exactly how to send data from C to Node.js. The solution demonstrates sending data from Node.js to C, but not in reverse. I have an application that requires two-way communications, so the missing component is critical for me.

我对writesendsendmsg之一应该能够完成工作的Unix套接字的理解,但是我没有任何运气.如果这种理解不正确,请提出建议.

My understanding of unix sockets that one of either write, send or sendmsg should be able to do the job, however, I am not having any luck. If this understanding is incorrect, please advise.

为了运行一个简单的示例,假设在C代码中读取一条消息时,让它发回一条消息并尝试触发节点服务器上的ipc.of[socketId].on('message',...)事件.

In order to get a trivial example running, lets say when a message is read in the C code, lets send back a message and try to trigger the ipc.of[socketId].on('message',...) event on the node server.

这意味着我正试图解决这个问题:

Which means I am trying to turn this:

while ( (rc=read(cl,buf,sizeof(buf))) > 0) {
      printf("read %u bytes: %.*s\n", rc, rc, buf);
}

对此:

while ( (rc=read(cl,buf,sizeof(buf)) ) > 0) {
  printf("read %u bytes: %.*s\n", rc, rc, buf);

  //Respond to the node server
  int n;
  char * msg = "{\"type\":\"message\",\"data\":\"hello response\"}\t";
  if((n = write(fd,msg,sizeof(msg))) < 0){
      printf("send failed\n");
  }else{
      printf("sent %d bytes\n", n);
  }
}

这意味着完整的 server.c 代码现在变为:

This would mean that the complete server.c code now becomes:

#include <stdio.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <stdlib.h>
#include <string.h> //Missing from original server.c

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

      //Respond to the node server
      int n;
      char * msg = "{\"type\":\"message\",\"data\":\"hello response\"}\t";
      if((n = write(fd,msg,sizeof(msg))) < 0){
          printf("send failed\n");
      }else{
          printf("sent %d bytes\n", n);
      }
    }

    if (rc == -1) {
      perror("read");
      exit(-1);
    }
    else if (rc == 0) {
      printf("EOF\n");
      close(cl);
    }
  }
  return 0;
}

现在,很不幸,对我来说,写消息返回代码-1,而node.js服务器未接收到该消息.

Now unfortunately, the write message for me returns code -1, and is not received by the node.js server.

client.js 代码保持不变,并且与原始问题中的代码相同.

The client.js code remains unchanged, and is as provided in the original question.

有人可以澄清我在做什么错吗?

Can someone please clarify what I am doing wrong?

推荐答案

您必须进行更改

char * msg = "{\"type\":\"message\",\"data\":\"hello response\"}\t";
if((n = write(fd,msg,sizeof(msg))) < 0){
    printf("send failed\n");
}else{
    printf("sent %d bytes\n", n);
}

char * msg = "{\"type\":\"message\",\"data\":\"hello response\"}\f";
if((n = write(cl,msg,strlen(msg))) < 0){
    printf("send failed\n");
}else{
    printf("sent %d bytes\n", n);
}

消息末尾,库正在等待 \ f :-)

The library is waiting for \f at the end of the message :-)

希望答案还不算太晚:-)

hope the answer is not too late :-)

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

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