我正在尝试通过 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;
和红宝石脚本
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