无法连接到redis服务器

时间:2013-03-09 17:22:33

标签: c redis

我无法使用credis_connect()连接到使用默认选项(127.0.0.1:6379)运行的redis服务器。这是我使用的测试代码:

#include <stdio.h>
#include "credis.h"

int main(int argc, char **argv)
{
    REDIS rh;
    char *val;
    int rc;


    printf("connecting to server at Port:6379\n");
    rh = credis_connect(NULL, 6379, 10000);

    if(rh == NULL)
    {
        printf("Error in connecting to server.\n");
        return -1;
    }
    printf("Connected to Redis Server. \n");

    /* ping server */
    rc = credis_ping(rh);
    printf("ping returned: %d\n", rc);


    /* set value of key "kalle" to "kula" */
    printf("Setting Key value to Redis Server.\n");
    credis_set(rh, "kalle", "kula");

    printf("Key value is set.\n");

      /* get value of key "kalle" */
    credis_get(rh, "kalle", &val);
    printf("get kalle returned: %s\n", val);

    /* close connection to redis server */
    credis_close(rh);

    return 0;
}

仅供参考:我在ubuntu 12.10上运行redis 2.6.10和0.2.3。

1 个答案:

答案 0 :(得分:0)

我不认为0-2-3可以使用现代Redis版本(2.6)。 credis 0-2-3于2010年发布,Redis已经发展了很多。

连接失败,因为credis需要在套接字连接后解析INFO命令的输出。目的是检索Redis服务器版本。由于INFO的输出已更改(现在包含用于隔离节的注释),因此无法提取版本,因此它会返回错误。

如果您想修复此特定问题(但可能还有许多其他问题......)您只需编辑credis.c源代码并替换:

int items = sscanf(rhnd->reply.bulk,
                   "redis_version:%d.%d.%d\r\n",
                   &(rhnd->version.major),
                   &(rhnd->version.minor),
                   &(rhnd->version.patch));

由:

int items = sscanf(rhnd->reply.bulk,
                   "# Server\nredis_version:%d.%d.%d\r\n",
                   &(rhnd->version.major),
                   &(rhnd->version.minor),
                   &(rhnd->version.patch));

我的建议是切换到hiredis,这是官方的C客户端。

相关问题