Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ##### C Code of the socket client
- #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);
- ##### Ruby Code of the socket server
- 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
Advertisement
Add Comment
Please, Sign In to add comment