K
K
Kamil Latypov2019-03-19 12:57:27
Redis
Kamil Latypov, 2019-03-19 12:57:27

How to make ZeroMQ+hiredis work?

Good day!
I am writing a client-server application, using the redis database + the hiredis library and communicating with the client through ZeroMQ, C language, Linux Debian, the radish server is included. The task is simple - the client sends a "Test" message, the server responds with "ok", while the radish is connected. If I solve it, I will add reactions to any requests to the radish, while I'm stuck at this stage. I am a beginner in these matters, so do not judge strictly. After the server is started, the client connects to it, any client message causes the server to crash with the message "Assertion failed: check () (src/msg.cpp:230)
Crash
"
Found out that this happens if there is a line "c = redisConnectWithTimeout(hostname , port, timeout);"
Redis connection works fine.

#include "czmq.h"
#include "stdio.h"
#include "unistd.h"
#include "string.h"
#include "assert.h"
#include "hiredis.h"
#include <stdlib.h>

int main (void)
{
    void *context = zmq_ctx_new ();
    void *server = zmq_socket (context, ZMQ_REP);
    int rc = zmq_bind (server, "tcp://*:5555");	

    redisContext *c;
    redisReply *reply;
    const char *hostname = "127.0.0.1";
    int port = 6379;

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);
    }

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

while (1) {
  zmq_msg_t msg;
  int rc = zmq_msg_init (&msg);
  assert (rc == 0);
  rc = zmq_msg_recv (&msg, server, 0);
  assert (rc != -1);	
  char src[rc];
  memcpy (src, zmq_msg_data(&msg), rc);
  printf("%s\n",src);
  sleep(1);
  zmq_msg_close (&msg);
  rc = zmq_msg_init_size (&msg, 3);
  assert (rc == 0);
        memcpy (zmq_msg_data (&msg), "ok\0",	3);
  rc = zmq_msg_send (&msg, server, ZMQ_DONTWAIT); 
  sleep(1);
}
    redisFree(c);
    return 0;
}

КОД КЛИЕНТА

#include "czmq.h"

int main (void)
{
    void *context = zmq_ctx_new ();
    void *client = zmq_socket (context, ZMQ_REQ);
    zmq_connect (client, "tcp://localhost:5555");
        zmq_msg_t msg;
  int rc = zmq_msg_init_size (&msg, 5);
  assert (rc == 0);
        memcpy (zmq_msg_data (&msg), "Test\0",	5);
  rc = zmq_msg_send (&msg, client, ZMQ_DONTWAIT); 
  sleep(1);
  rc = zmq_msg_init (&msg);
  assert (rc == 0);
  rc = zmq_msg_recv (&msg, client, 0);
  assert (rc != -1);
  char txt[sizeof(zmq_msg_data(&msg))];
  strcpy (txt, zmq_msg_data(&msg));
  printf("%s\n",txt);
 return 0;
    zmq_close (client);
    zmq_ctx_destroy (context);
zmq_msg_close(&msg);
}

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question