Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我尝试在Ruby和C之间编写unix域套接字通信脚本_C_Ruby_Sockets_Unix_Unix Socket - Fatal编程技术网

我尝试在Ruby和C之间编写unix域套接字通信脚本

我尝试在Ruby和C之间编写unix域套接字通信脚本,c,ruby,sockets,unix,unix-socket,C,Ruby,Sockets,Unix,Unix Socket,我正试图通过unix域套接字在Ruby脚本作为服务器和C应用程序作为客户端之间搭建一座桥梁,但目前我陷入了读取循环,直到我杀死Ruby脚本,也许有人可以看看代码 struct sockaddr_un addr; int fd,rc; char *socket_path = "/tmp/auth.sock"; char *output_packet = cern_format_packet(pw, key); if ( (fd = socket(AF_UNIX, SO

我正试图通过unix域套接字在Ruby脚本作为服务器和C应用程序作为客户端之间搭建一座桥梁,但目前我陷入了读取循环,直到我杀死Ruby脚本,也许有人可以看看代码

    struct sockaddr_un addr;
  int fd,rc;
  char *socket_path = "/tmp/auth.sock";

  char *output_packet = cern_format_packet(pw, key);


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

  memset(&addr, 0, sizeof(addr));
  addr.sun_family = AF_UNIX;
  strncpy(addr.sun_path, socket_path, sizeof(addr.sun_path)-1);

  if (connect(fd, (struct sockaddr*)&addr, sizeof(addr)) == -1) {
    error("connect error");
    return 0;
  }
  logit("cern_output: %s", output_packet);
  write(fd, output_packet, strlen(output_packet));

#define BUFFERSIZE 1024
#define MAXDATASIZE 256   
  int c, q = 0, success;
  char* input_buffer;
  char data_buffer[MAXDATASIZE];
  size_t input_buffer_size = BUFFERSIZE;
  input_buffer = malloc(input_buffer_size);

  if (input_buffer)
  {
      while((c = read(fd, data_buffer, MAXDATASIZE)) > 0)
      {
          if (c + q > input_buffer_size)
          {
              input_buffer_size *= 2; /* Arbitrary doubling of size. */
              char* tmp = realloc(input_buffer, input_buffer_size);
              if (tmp)
              {
                  input_buffer = tmp;
              }
              else
              {
                  /* memory allocation failure. */
                  free(input_buffer);
                  input_buffer = 0;
                  break;
              }
          }
          memcpy(input_buffer + q, data_buffer, c);
          q += c;
      }
  }
  json_error_t error; 
  json_t *root = json_loads( input_buffer, 0, &error );
  if (root) {
    json_t *json_status = json_object_get( root, "status" );
    json_t *json_command = json_object_get( root, "command" );
    const char *return_comamnd = json_string_value( json_command );
    success = (int)json_integer_value(json_status);
    logit("json_response: status: %i command: %s",success, return_comamnd);
  }


  close(fd);
  return success;
还有ruby脚本

require "socket"
require 'msgpack'
require "json"

file = "/tmp/auth.sock"

File.unlink if File.exists?(file) && File.socket?(file)

serv = UNIXServer.new(file)

loop do
        s = serv.accept
        msg = s.gets
        msg = msg.gsub("\n", "") if msg
        p JSON.parse(msg)
        a = {}
        a["status"] = 1
        a["command"] = "hello world"
        s.write(JSON.generate(a))
s.close
end

客户端对
read()
的调用很可能会阻塞,因为服务器发送的数据少于客户端被告知读取的数据,即
MAXDATASIZE
字节

一定要避开这个问题,让服务器在关闭套接字之前有序地关闭它

我不是Ruby专家,但可能是:

s.shutdown
s.close
我会做的


此外,在该行的服务器中:

msg = s.gets

可能会一直阻止,直到收到
\n
。(有关
获取的详细信息,请参见此处:)

更改为8,但运气仍然不好…:(@AurimasNiekis:对于测试,请尝试
1
。和
printf()
每次调用
read()
都会收到什么。我在所有代码周围添加了调试信息,它会阻塞while循环,甚至不会cycle@AurimasNiekis:它在对
read()
的第一次调用中使用
read()阻塞
被告知准确读取
1
字节?在客户端:您可能希望添加代码以测试先前对
write()
的调用是否成功。此外,
write()
在调用
wite()之后,还会输出最后一个
\n
您已经有了。请查看我的更新答案。我认为问题出在ruby脚本上,因为在while循环中,它无法读取任何内容