Allow connections to unix socket in example
To minimize code changes, a simple `u` (or UNIX, Unix, unix, etc -- as long as the first character is u or U) is used as a marker for the 'port' argument. In this case, the hostname is interpreted to be the path to the unix socket.
This commit is contained in:
parent
91289c81ee
commit
8633a2f32e
@ -5,14 +5,27 @@
|
|||||||
#include <hiredis.h>
|
#include <hiredis.h>
|
||||||
|
|
||||||
int main(int argc, char **argv) {
|
int main(int argc, char **argv) {
|
||||||
unsigned int j;
|
unsigned int j, isunix = 0;
|
||||||
redisContext *c;
|
redisContext *c;
|
||||||
redisReply *reply;
|
redisReply *reply;
|
||||||
const char *hostname = (argc > 1) ? argv[1] : "127.0.0.1";
|
const char *hostname = (argc > 1) ? argv[1] : "127.0.0.1";
|
||||||
|
|
||||||
|
if (argc > 2) {
|
||||||
|
if (*argv[2] == 'u' || *argv[2] == 'U') {
|
||||||
|
isunix = 1;
|
||||||
|
/* in this case, host is the path to the unix socket */
|
||||||
|
printf("Will connect to unix socket @%s\n", hostname);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
int port = (argc > 2) ? atoi(argv[2]) : 6379;
|
int port = (argc > 2) ? atoi(argv[2]) : 6379;
|
||||||
|
|
||||||
struct timeval timeout = { 1, 500000 }; // 1.5 seconds
|
struct timeval timeout = { 1, 500000 }; // 1.5 seconds
|
||||||
|
if (isunix) {
|
||||||
|
c = redisConnectUnixWithTimeout(hostname, timeout);
|
||||||
|
} else {
|
||||||
c = redisConnectWithTimeout(hostname, port, timeout);
|
c = redisConnectWithTimeout(hostname, port, timeout);
|
||||||
|
}
|
||||||
if (c == NULL || c->err) {
|
if (c == NULL || c->err) {
|
||||||
if (c) {
|
if (c) {
|
||||||
printf("Connection error: %s\n", c->errstr);
|
printf("Connection error: %s\n", c->errstr);
|
||||||
|
Loading…
Reference in New Issue
Block a user