hiredis 命令对简单 C 代码的空回复

Null reply from hiredis command on simple C code

我在 Kali Linux 2019.4:

上使用以下命令安装了 Redis 服务器
$ 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

然后在 redis-cli 上,当我输入 PING 时,它回复 PONG

但是当我使用下面的 C 代码并使用 hiredis 时,它会回复 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);

} 

输出:PING: (null)

有人可以帮我吗?

首先,你运行 Redis 作为后台服务在正确的端口上吗?

我认为如果你想从新事物开始,最好从那些已经测试过的例子开始。而且,如果您再次遇到错误,可能是您的 Redis 新手没有真正完成。

请仔细查看以下链接并多加注意这些示例:

https://github.com/redis/hiredis

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

希望对您有所帮助。