tencent cloud

云数据库 Tendis

产品动态
产品简介
产品概述
产品优势
应用场景
产品系列
产品性能
命令兼容性
地域和可用区
相关概念
相关产品
购买指南
计费概述
产品定价
欠费说明
快速入门
创建 Tendis 实例
连接 Tendis 实例
iptable 转发
操作指南
多语言连接
维护管理实例
监控功能
配置安全组
禁用命令
常见问题
使用常见问题
购买相关问题
连接登录问题
联系我们
Tendis 政策
隐私政策
数据处理和安全协议
词汇表

C 连接示例

PDF
聚焦模式
字号
最后更新时间: 2024-12-19 20:23:34
运行前必备: 下载并安装 hiredis
示例代码
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <hiredis.h>

int main(int argc, char **argv) {
unsigned int j;
redisContext *c;
redisReply *reply;

if (argc < 4) {
printf("Usage: 192.xx.xx.195 6379 instance_id password\\n");
exit(0);
}
const char *hostname = argv[1];
const int port = atoi(argv[2]);
const char *instance_id = argv[3];
const char *password = argv[4];


struct timeval timeout = { 1, 500000 }; // 1.5 seconds
c = redisConnectWithTimeout(hostname, port, timeout);
if (c == NULL || c->err) {
if (c) {
printf("Connection error: %s\\n", c->errstr);
redisFree(c);
} else {
printf("Connection error: can't allocate redis context\\n");
}
exit(1);
}

/* AUTH */
reply = redisCommand(c, "AUTH %s", password);
printf("AUTH: %s\\n", reply->str);
freeReplyObject(reply);

/* PING server */
reply = redisCommand(c,"PING");
printf("PING: %s\\n", reply->str);
freeReplyObject(reply);

/* Set a key */
reply = redisCommand(c,"SET %s %s", "name", "credis_test");
printf("SET: %s\\n", reply->str);
freeReplyObject(reply);

/* Try a GET */
reply = redisCommand(c,"GET name");
printf("GET name: %s\\n", reply->str);
freeReplyObject(reply);

/* Disconnects and frees the context */
redisFree(c);

return 0;
}
运行结果


帮助和支持

本页内容是否解决了您的问题?

填写满意度调查问卷,共创更好文档体验。

文档反馈