【问题标题】:Unable to connect to redis server无法连接到redis服务器
【发布时间】:2013-02-25 03:48:55
【问题描述】:

我无法使用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 和 credis 0.2.3。

【问题讨论】:

  • 您是否从使用 CLI 运行您的代码的同一台机器成功连接?

标签: c redis


【解决方案1】:

我认为 credis 0-2-3 不能与现代 Redis 版本 (2.6) 一起使用。 credis 0-2-3 于 2010 年发布,Redis 发展了很多。

连接失败,因为 credis 需要在套接字连接后解析 INFO 命令的输出。目的是检索 Redis 服务器版本。因为 INFO 的输出已经改变(它现在包括 cmets 来隔离部分),credis 不再能够提取版本,所以它返回一个错误。

如果您想解决这个特定问题(但可能还有很多其他问题...),您只需编辑 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 客户端。

【讨论】:

    猜你喜欢
    • 2017-03-01
    • 1970-01-01
    • 2019-10-18
    • 2021-10-25
    • 1970-01-01
    • 1970-01-01
    • 2023-02-11
    • 2021-09-02
    • 1970-01-01
    相关资源
    最近更新 更多