Skip to content

Commit

Permalink
issue 270 showing SUB POLLIN error
Browse files Browse the repository at this point in the history
  • Loading branch information
Chuck Remes committed Oct 17, 2011
1 parent a172a19 commit 196c1c0
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 0 deletions.
9 changes: 9 additions & 0 deletions 270/3-0/README
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,9 @@
A SUB socket should have its socket EVENTS set to
POLLIN when it has a message in queue. This works
when the SUB socket *connects* and the PUB socket
binds, but it FAILS when the SUB socket *binds* and
the PUB socket connects.

issue.c shows the failure.

issue_works.c shows how it works with the connect/bind swapped
45 changes: 45 additions & 0 deletions 270/3-0/issue.c
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,45 @@
//
// A SUB socket should always have ZMQ_POLLIN set
// when it has a message in queue and ready to read.
// FAILS when the SUB socket binds the socket.
//
// gcc -lzmq issue.c
// chmod +x a.out
// ./a.out
//
#include <zmq.h>

#include <assert.h>
#include <stdio.h>



int main (void)
{
int rc;
void *context = zmq_init (1);
void *sender = zmq_socket (context, ZMQ_PUB);
void *receiver = zmq_socket (context, ZMQ_SUB);
assert (0 == zmq_setsockopt (receiver, ZMQ_SUBSCRIBE, "", 0));
assert (0 == zmq_bind (receiver, "tcp://127.0.0.1:7557"));
assert (0 == zmq_connect (sender, "tcp://127.0.0.1:7557"));
sleep (1);

zmq_msg_t m1, m2;
assert (0 == zmq_msg_init_data (&m1, "Hello world", 11, NULL, NULL));
assert (11 == zmq_sendmsg(sender, &m1, 0));
sleep (1);

unsigned int events;
size_t size = sizeof (events);
assert (0 == zmq_getsockopt(receiver, ZMQ_EVENTS, &events, &size));

// the test!
fprintf (stderr, "events [%d], pollin [%d]\n", events, ZMQ_POLLIN);
assert (events == ZMQ_POLLIN);

zmq_close (sender);
zmq_close (receiver);
zmq_term (context);
return (0);
}
45 changes: 45 additions & 0 deletions 270/3-0/issue_works.c
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,45 @@
//
// A SUB socket should always have ZMQ_POLLIN set
// when it has a message in queue and ready to read.
// SUCCEEDS only when the SUB socket connects (not binds).
//
// gcc -lzmq issue_works.c
// chmod +x a.out
// ./a.out
//
#include <zmq.h>

#include <assert.h>
#include <stdio.h>



int main (void)
{
int rc;
void *context = zmq_init (1);
void *sender = zmq_socket (context, ZMQ_PUB);
void *receiver = zmq_socket (context, ZMQ_SUB);
assert (0 == zmq_setsockopt (receiver, ZMQ_SUBSCRIBE, "", 0));
assert (0 == zmq_connect (receiver, "tcp://127.0.0.1:7557"));
assert (0 == zmq_bind (sender, "tcp://127.0.0.1:7557"));
sleep (1);

zmq_msg_t m1, m2;
assert (0 == zmq_msg_init_data (&m1, "Hello world", 11, NULL, NULL));
assert (11 == zmq_sendmsg(sender, &m1, 0));
sleep (1);

unsigned int events;
size_t size = sizeof (events);
assert (0 == zmq_getsockopt(receiver, ZMQ_EVENTS, &events, &size));

// the test!
fprintf (stderr, "events [%d], pollin [%d]\n", events, ZMQ_POLLIN);
assert (events == ZMQ_POLLIN);

zmq_close (sender);
zmq_close (receiver);
zmq_term (context);
return (0);
}

0 comments on commit 196c1c0

Please sign in to comment.