`
lobin
  • 浏览: 382725 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

Redis: 客户端

 
阅读更多

客户端

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <hiredis.h>

typedef struct 
{
  char *data;
  int size;

  int r_offs;
} buffer_t;

#define INIT_SIZE 8

int buffer_init(buffer_t *buffer, int initial_size)
{
  buffer->data = (char *) malloc(initial_size);
  if (buffer->data == NULL)
  {
    return -1;
  }
  memset(buffer->data, 0, initial_size);

  buffer->size = initial_size;
  buffer->r_offs = 0;
  return 0;
}

void buffer_clear(buffer_t *buffer)
{
  memset(buffer->data, 0, buffer->size);
  buffer->r_offs = 0;
}

void buffer_destroy(buffer_t *buffer)
{
  free(buffer->data);
  buffer->size = 0;
  buffer->r_offs = 0;
}

void stdin_prefix(char* host, int port, redisContext *redisContext)
{
  if (redisContext && ! redisContext->err)
  {
    printf("%s:%d> ", host, port);
  }
  else
  {
    printf("not connected> ");
  }
}

int main(int argc, const char **argv)
{
  redisContext *redisContext;
  redisReply *redisReply;

  char *host = "127.0.0.1";
  int port = 6379;

  int repl = 1;

  int i;
  for (i = 1; i < argc; i++)
  {
    const char *arg = argv[i];
    if (strncmp(arg, "-h", 2) == 0)
    {
      if (i + 1 < argc)
      {
        host = (char *) argv[i + 1];
        i++;
      }
    }
    else if (strncmp(arg, "-p", 2) == 0)
    {
      if (i + 1 < argc)
      {
        port = atoi(argv[i + 1]);
        i++;
      }
    }
    else 
    {
      repl = 0;
      argc = argc - i;
      argv = &argv[i];
    }
  }
  //printf("host:port=%s:%d\n", host, port);

  redisContext = redisConnect(host, port);
  if (! redisContext)
  {
    printf("Could not connect to Redis at %s:%d: (redisConnect err.)\n", host, port);
    if (! repl)
    {
      return -1;
    }
  }
  if (redisContext->err)
  {
    printf("Could not connect to Redis at %s:%d: %s(redisConnect err=%d.)\n", host, port, redisContext->errstr, redisContext->err);
    if (! repl)
    {
      redisFree(redisContext);
      return -1;
    }
  }
  if (! repl)
  {
    redisReply = redisCommandArgv(redisContext, argc, argv, NULL);
    printf("%s\n", redisReply->str);
    freeReplyObject(redisReply);
  }
  else 
  {
    buffer_t buffer;
    buffer_init(&buffer, INIT_SIZE);

    for (; ;)
    {
      stdin_prefix(host, port, redisContext);

      while(1)
      {
        int i, r_offs = buffer.r_offs;

        fgets(buffer.data + buffer.r_offs, buffer.size - buffer.r_offs, stdin);
        buffer.r_offs += buffer.size - buffer.r_offs - 1;
        for (i = r_offs; i < buffer.r_offs; i++)
        {
          if (buffer.data[i] == '\n')
          {
            buffer.data[i] = 0;
            break;
          }
        }
        if (i < buffer.r_offs)
        {
          break;
        }
        else
        {
          buffer.size += INIT_SIZE;
          buffer.data = realloc(buffer.data, buffer.size);
        }
      }

      if (strcmp(buffer.data, "quit") == 0)
      {
        break;
      }

      if (! redisContext || redisContext->err)
      {
        if (redisContext)
        {
          printf("Could not connect to Redis at %s:%d: %s(redisConnect err=%d.)\n", host, port, redisContext->errstr, redisContext->err);
          redisFree(redisContext);
        }
        else
        {
          printf("Could not connect to Redis at %s:%d: (redisConnect err.)\n", host, port);
        }
        redisContext = redisConnect(host, port);
        buffer_clear(&buffer);
        continue;
      }

      redisReply = redisCommand(redisContext, buffer.data);
      if (redisReply->str)
      {
        printf("%s\n", redisReply->str);
      }
      else 
      {
        printf("(nil)\n");
      }
      freeReplyObject(redisReply);
      buffer_clear(&buffer);

      /*
      redisReply = redisCommand(redisContext, "SET %s %s", "hello", "this is c");
      printf("%s\n", redisReply->str);
      freeReplyObject(redisReply);

      redisReply = redisCommand(redisContext, "GET %s", "hello");
      printf("%s\n", redisReply->str);
      freeReplyObject(redisReply);

      redisReply = redisCommand(redisContext, "GET hel");
      if (redisReply->str)
      {
        printf("%s\n", redisReply->str);
      }
      else 
      {
        printf("(nil)\n");
      }
      
      freeReplyObject(redisReply);
      */
    }
    buffer_destroy(&buffer);
  }
  redisFree(redisContext);
  return 0;
}

 

>.\redis_test.exe -h 127.0.0.1

Could not connect to Redis at 127.0.0.1:6379: Connection refused(redis

Connect err=1.)

not connected> quit

 

>.\redis_test.exe -h 127.0.0.1 -p 1234

 

Could not connect to Redis at 127.0.0.1:1234: Connection refused(redis

Connect err=1.)

not connected> quit

 

>.\redis_test.exe -h 127.0.0.1

Could not connect to Redis at 127.0.0.1:6379: Connection refused(redis

Connect err=1.)

not connected> get hello

Could not connect to Redis at 127.0.0.1:6379: Connection refused(redis

Connect err=1.)

not connected> get name

Could not connect to Redis at 127.0.0.1:6379: Connection refused(redis

Connect err=1.)

not connected> quit

 

>.\redis_test.exe -h 192.168.0.102

192.168.0.102:6379> quit

 

>.\redis_test.exe -h 192.168.0.102

192.168.0.102:6379> get hello

this is c

192.168.0.102:6379> get keya

valuea

192.168.0.102:6379> get name

luoxiaoyong

192.168.0.102:6379> set key b

OK

192.168.0.102:6379> get key

b

192.168.0.102:6379> quit

 

0
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics