无法连接到redis服务器

时间:2022-02-04 15:36:42

I am unable to connect to redis server running with default options (127.0.0.1:6379) using credis_connect(). Here is the test code I used:

我无法使用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;
}

FYI: I am running redis 2.6.10 and credis 0.2.3 on ubuntu 12.10.

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

1 个解决方案

#1


0  

I don't think credis 0-2-3 can work with modern Redis version (2.6). credis 0-2-3 was released in 2010, and Redis has evolved a lot.

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

Connection fails because credis needs to parse the output of the INFO command just after socket connection. Purpose is to retrieve the Redis server version. Because the output of INFO has changed (it is now including comments to isolate sections), credis is not able anymore to extract the version, so it returns an error.

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

If you want to fix this specific issue (but there may be many others ...) you just have to edit credis.c source code and replace:

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

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

by:

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

My suggestion would be to switch to hiredis, which is the official C client.

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

#1


0  

I don't think credis 0-2-3 can work with modern Redis version (2.6). credis 0-2-3 was released in 2010, and Redis has evolved a lot.

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

Connection fails because credis needs to parse the output of the INFO command just after socket connection. Purpose is to retrieve the Redis server version. Because the output of INFO has changed (it is now including comments to isolate sections), credis is not able anymore to extract the version, so it returns an error.

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

If you want to fix this specific issue (but there may be many others ...) you just have to edit credis.c source code and replace:

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

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

by:

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

My suggestion would be to switch to hiredis, which is the official C client.

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