无法使用hiredis C库连接到redis服务器

时间:2018-09-28 05:50:20

标签: c++ redis dev-c++ hiredis

我正在尝试使用下面提到的C ++程序连接到本地运行的Redis服务器。但是在服务器的控制台上看不到任何客户端已连接,尽管我在客户端也没有任何错误。也无法接收命令的响应。我需要改变我做这些东西的方式吗?

我正在使用https://github.com/dmajkic/redis/downloads处的预编译库。

代码:

#include "hiredis.h"
#include <iostream>
using namespace std;

int main()
{
   redisContext *c;
   c = redisConnect("127.0.0.1", 6379);
    if (c == NULL || c->err) {
        if (c) {
            printf("Error: %s\n", c->errstr);
            // handle error
        } else {
            printf("Can't allocate redis context\n");
        }
    }
    else{
    cout << "\nThere is no error";
    void *reply = redisCommand(c, "SET foo bar");
    if(reply)
    cout << "\ngot a reply";
    else
    cout << "\ncould not get any reply";

    }
return 0;
}

输出: 没有错误

无法得到任何答复

但是使用redis-cli.exe我可以连接到服务器。想知道我会做错什么。我正在使用带有TDM-GCC 4.9.2 64位版本的dev-C ++ 5.11

0 个答案:

没有答案
相关问题