[zeromq-dev] [zeromq/2.0 - beginner's question] why do I get a seg fault?

Matias GUIJARRO matias.guijarro at esrf.fr
Mon Nov 16 16:41:55 CET 2009


Hi there,

I am starting to evaluate zeromq, and I found interesting to test the 
2.0 version
(even if it is still in alpha state ?). From the web page, it seems the 
2.0 version
is much better than the actual stable release.

I wanted to write a dummy client and a dummy server, just peer-to-peer. I am
programing on a Linux box. I would like to get your inputs on how to 
improve the
following code and I would like to know why I manage to get seg faults :

/* server code */
#include <zmq.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void no_deallocation(void* ignored) {};

int main(char** argv) {
  void *ctx;
  void *s;
  zmq_msg_t *msg;
  ctx = zmq_init(1,1,0);
  s = zmq_socket(ctx, ZMQ_PUB);
  zmq_bind(s, "tcp://127.0.0.1:5555");
  zmq_msg_init_size(msg, 512);

  char string[512];
  while (1) {
    printf("Please type message to send (empty=exit) : ");
    gets(string);
    if (string[0]=='\0') {
      zmq_term(ctx);
      return(0);
    }
    int msg_len = strlen(string);
    printf("   ... sending %d bytes message\n", msg_len);
    zmq_msg_init_data(msg, string, msg_len, no_deallocation);
    printf("    - %d\n", zmq_send(s, msg, 0));
  }
}

/* client code */
#include <zmq.h>
#include <stdio.h>
#include <signal.h>

static int should_exit;

void no_deallocation(void* ignored) {};

void signal_handler(int signum) {
  switch (signum) {
  case SIGINT:
    should_exit=1;
    break;
  default:
    break;
  }
}

int main(char** argv) {
  should_exit = 0;

  struct sigaction a;
  a.sa_handler = signal_handler;
  sigemptyset(&a.sa_mask);
  sigaction(SIGINT, &a, NULL);

  void *ctx;
  void *s;
  zmq_msg_t msg;
 
  ctx = zmq_init(1,1,0);
  s = zmq_socket(ctx, ZMQ_SUB);

  printf("Connecting to 127.0.0.1:5555\n");
  zmq_connect(s, "tcp://127.0.0.1:5555");

  printf("Subscribing to receive any kind of message\n");
  zmq_setsockopt(s, ZMQ_SUBSCRIBE, "*", 1);

  while (! should_exit) {
    zmq_msg_init(&msg);
    if (zmq_recv(s, &msg, ZMQ_NOBLOCK) < 0) continue;
    printf("Received: %s\n", zmq_msg_data(&msg));
  }
  printf("Goodbye.\n");
  zmq_term(ctx);
}

The seg fault occur when : 1) I start the server program, 2) I start
the client program, 3) I send some messages from server to client,
4) I close the client, 5) I restart the client, 6) I send another message =>
crash.

What am I missing ? I thought that reconnections etc. should
all be handled by the library.

Another thing : currently the receiver program is killing my CPU because
of the while loop. I wanted to use polling instead (with the right context
initialization with the ZMQ_POLL flag and the appropriate call to zmq_poll),
but I couldn't because "zmq_pollitem_t" is undefined in zmq.h...

Please help !
By the way... Any advice/feedback will be useful for me.

Thanks a lot in advance,
Matias.









More information about the zeromq-dev mailing list