[zeromq-dev] assert error on very simple C client/server example
Marc Ilgen
marc at westofpluto.com
Sat Jul 16 22:47:12 CEST 2011
Hello
I am testing out 0MQ, starting with the most basic HelloWorld client/server
example on VisualC++2008. The problem is that in closing the client in the
way the guide tells you to, I get a fatal rc == 0 assert error.
Here's what I did. In order to get everything to compile and link, I had to
actually include the 0MQ library project (3.0 distribution) into my user
solution. So my user solution HelloWorld containst 3 projects:
HelloZMQServer, HelloZMQClient, and the ZeroMQ project.
HelloZMQServer contains a single file helloserver.c as follows:
//
// Hello World server
// Binds REP socket to tcp://*:5555
// Expects "Hello" from client, replies with "World"
//
#include <zmq.h>
#include <stdio.h>
#include <string.h>
int main (void)
{
zmq_msg_t request;
zmq_msg_t reply;
void *context = zmq_init (1);
// Socket to talk to clients
void *responder = zmq_socket (context, ZMQ_REP);
zmq_bind (responder, "tcp://*:5555");
while (1) {
// Wait for next request from client
zmq_msg_init (&request);
zmq_recvmsg (responder, &request, 0);
printf ("Received Hello\n");
zmq_msg_close (&request);
// Do some 'work'
Sleep (1);
// Send reply back to client
zmq_msg_init_size (&reply, 5);
memcpy (zmq_msg_data (&reply), "World", 5);
zmq_sendmsg (responder, &reply, 0);
zmq_msg_close (&reply);
}
// We never get here but if we did, this would be how we end
zmq_close (responder);
zmq_term (context);
return 0;
}
HelloZMQClient contains a single file helloclient.c as follows:
//
// Hello World client
// Connects REQ socket to tcp://localhost:5555
// Sends "Hello" to server, expects "World" back
//
#include <zmq.h>
#include <string.h>
#include <stdio.h>
int main (void)
{
int zero = 0;
int request_nbr;
zmq_msg_t request;
zmq_msg_t reply;
void *requester=NULL;
void *context = NULL;
context = zmq_init (1);
// Socket to talk to server
printf ("Connecting to hello world server.\n");
requester = zmq_socket (context, ZMQ_REQ);
zmq_connect (requester, "tcp://localhost:5555");
for (request_nbr = 0; request_nbr != 10; request_nbr++) {
zmq_msg_init_size (&request, 5);
memcpy (zmq_msg_data (&request), "Hello", 5);
printf ("Sending Hello %d\n", request_nbr);
zmq_sendmsg (requester, &request, 0);
zmq_msg_close (&request);
zmq_msg_init (&reply);
zmq_recvmsg (requester, &reply, 0);
printf ("Received World %d\n", request_nbr);
zmq_msg_close (&reply);
}
printf ("In Client, end of loop, just before close\n");
zmq_close (requester);
printf ("In Client, after zmq_close\n");
zmq_term (context);
printf ("In Client, after zmq_term\n");
return 0;
}
The server seems to run OK because it is in an infinite loop. The client
however runs through its loop of 10 iterations and then crashes with an
assert error rc == 0 in ctx.cpp somewhere inside the zmq_term() call.
All I wish to do in this very simple example is terminate the client after
it does stuff. The above client code is what the documentation says to use
to terminate the client, but it throws this abnormal fatal crash.
I thought from this page: http://zguide.zeromq.org/pdf-c:chapter1
That maybe I had to do something with zmq_setsockopt:
zmq_setsockopt (mysocket, ZMQ_LINGER, &zero, sizeof (zero));
but this doesn't help.
So what do I do to fix this problem?
Thanks
----------------------------------------------------------------------------
------
Marc Ilgen
marc at westofpluto.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.zeromq.org/pipermail/zeromq-dev/attachments/20110716/39c5c8b3/attachment.htm>
More information about the zeromq-dev
mailing list