c - zmq_socket() is giving segmentation fault -
i learning zeromq , have following test code:
void *context = (void *)zmq_ctx_new(); if (context == null) { printf("context null\n"); } else { printf("context created successfully\n"); } printf("connecting 0mq server\n"); void *responder = zmq_socket (context, zmq_req); printf("got socket\n"); if (responder == null) { printf("responder null\n"); } else { printf("responder created successfully\n"); }
when run code, crashes when zmq_socket()
called. here output:
starting 0mq server context created connecting 0mq server segmentation fault (core dumped)
i'm not sure why zmq_socket()
fails. have tried move zmq library in beginning of linking series in makefile. still fails.
any highly appreciated.
change
void context = (void)zmq_ctx_new();
into
void *context = (void *)zmq_ctx_new();
and, tried code, void context = (void) zmq_ctx_new() cause compile error.
here code can work on osx.
gcc -o cli client.c -lzmq gcc -o srv server.c -lzmq
client.c:
#include <zmq.h> #include <string.h> #include <stdio.h> #include <unistd.h> int main (void) { printf ("connecting hello world server…\n"); void *context = (void *)zmq_ctx_new (); void *requester = zmq_socket (context, zmq_req); zmq_connect (requester, "tcp://localhost:5555"); int request_nbr; (request_nbr = 0; request_nbr != 10; request_nbr++) { char buffer [10]; char snd[] = "hello"; printf ("sending hello %d…\n", request_nbr); zmq_send (requester, snd, sizeof(snd), 0); zmq_recv (requester, buffer, 10, 0); printf ("received world %d\n", request_nbr); } zmq_close (requester); zmq_ctx_destroy (context); return 0; }
server.c:
#include <zmq.h> #include <stdio.h> #include <unistd.h> #include <string.h> #include <assert.h> int main (void) { // socket talk clients void *context = zmq_ctx_new (); void *responder = zmq_socket (context, zmq_rep); int rc = zmq_bind (responder, "tcp://*:5555"); assert (rc == 0); while (1) { int nrecv; char buffer [10]; nrecv = zmq_recv (responder, buffer, 10, 0); printf ("[%d] received %s\n", nrecv, buffer); sleep (1); // 'work' zmq_send (responder, "world", 5, 0); } return 0; }
Comments
Post a Comment