credishiredis

Null reply from hiredis command on simple C code


I installed Redis server using below commands on Kali Linux 2019.4:

$ redisurl="http://download.redis.io/redis-stable.tar.gz"
$ curl -s -o redis-stable.tar.gz $redisurl
$ sudo su root
$ mkdir -p /usr/local/lib/
$ chmod a+w /usr/local/lib/
$ tar -C /usr/local/lib/ -xzf redis-stable.tar.gz
$ rm redis-stable.tar.gz
$ cd /usr/local/lib/redis-stable/
$ make && make install

and then on redis-cli when I Enter PING, it replies PONG

But when i use below C code with using hiredis, it replies null.

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

#include <hiredis/hiredis.h>

redisContext *c;
redisReply *reply;
void main() {

    c = redisConnect("127.0.0.1", 6379);
    reply = redisCommand(c,"PING");
    printf("PING: %s \n", reply->str);

} 

The output: PING: (null)

Can anybody help me?


Solution

  • First of all, Do you run Redis as a service in the background on the correct port?

    I think if you want to start with new things, it's better, to begin with, those examples that were already tested. And, If you got an error again probably your Redis upstarting not truly done.

    Please look at the below links carefully and pay more attention to those examples:

    https://github.com/redis/hiredis

    https://github.com/redis/hiredis/tree/master/examples

    I hope it could be helpful.