Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
ruanhaishen
hiredis
Commits
ba947bc9
Commit
ba947bc9
authored
Nov 27, 2017
by
Mark Nunberg
Browse files
Add SSL example
parent
5f50eb41
Changes
1
Hide whitespace changes
Inline
Side-by-side
examples/example-ssl.c
0 → 100644
View file @
ba947bc9
#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: %s <host> <port> <cert> <key> [ca]
\n
"
,
argv
[
0
]);
exit
(
1
);
}
const
char
*
hostname
=
(
argc
>
1
)
?
argv
[
1
]
:
"127.0.0.1"
;
int
port
=
atoi
(
argv
[
2
]);
const
char
*
cert
=
argv
[
3
];
const
char
*
key
=
argv
[
4
];
const
char
*
ca
=
argc
>
4
?
argv
[
5
]
:
NULL
;
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
);
}
if
(
redisSecureConnection
(
c
,
ca
,
cert
,
key
)
!=
REDIS_OK
)
{
printf
(
"Couldn't initialize SSL!
\n
"
);
printf
(
"Error: %s
\n
"
,
c
->
errstr
);
redisFree
(
c
);
exit
(
1
);
}
/* PING server */
reply
=
redisCommand
(
c
,
"PING"
);
printf
(
"PING: %s
\n
"
,
reply
->
str
);
freeReplyObject
(
reply
);
/* Set a key */
reply
=
redisCommand
(
c
,
"SET %s %s"
,
"foo"
,
"hello world"
);
printf
(
"SET: %s
\n
"
,
reply
->
str
);
freeReplyObject
(
reply
);
/* Set a key using binary safe API */
reply
=
redisCommand
(
c
,
"SET %b %b"
,
"bar"
,
(
size_t
)
3
,
"hello"
,
(
size_t
)
5
);
printf
(
"SET (binary API): %s
\n
"
,
reply
->
str
);
freeReplyObject
(
reply
);
/* Try a GET and two INCR */
reply
=
redisCommand
(
c
,
"GET foo"
);
printf
(
"GET foo: %s
\n
"
,
reply
->
str
);
freeReplyObject
(
reply
);
reply
=
redisCommand
(
c
,
"INCR counter"
);
printf
(
"INCR counter: %lld
\n
"
,
reply
->
integer
);
freeReplyObject
(
reply
);
/* again ... */
reply
=
redisCommand
(
c
,
"INCR counter"
);
printf
(
"INCR counter: %lld
\n
"
,
reply
->
integer
);
freeReplyObject
(
reply
);
/* Create a list of numbers, from 0 to 9 */
reply
=
redisCommand
(
c
,
"DEL mylist"
);
freeReplyObject
(
reply
);
for
(
j
=
0
;
j
<
10
;
j
++
)
{
char
buf
[
64
];
snprintf
(
buf
,
64
,
"%u"
,
j
);
reply
=
redisCommand
(
c
,
"LPUSH mylist element-%s"
,
buf
);
freeReplyObject
(
reply
);
}
/* Let's check what we have inside the list */
reply
=
redisCommand
(
c
,
"LRANGE mylist 0 -1"
);
if
(
reply
->
type
==
REDIS_REPLY_ARRAY
)
{
for
(
j
=
0
;
j
<
reply
->
elements
;
j
++
)
{
printf
(
"%u) %s
\n
"
,
j
,
reply
->
element
[
j
]
->
str
);
}
}
freeReplyObject
(
reply
);
/* Disconnects and frees the context */
redisFree
(
c
);
return
0
;
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment