[zeromq-dev] Problem using inproc - What am I doing wrong?

Iggy Philly iggy.philly at hotmail.com
Fri Aug 24 23:54:02 CEST 2012





I'm trying a simple example using inproc but I'm obviously doing something wrong. My publisher socket sends a message out and the subscriber is polling but never sees anything. I'm sure it's something silly. Here's my code: #include <zmq.h>
#include <iostream>
#include <memory.h>
#include <assert.h>using namespace std;static void* startTestThread(void* context)
{
  void* socket = zmq_socket(context, ZMQ_SUB);
 assert(socket);
 int ret = zmq_connect(socket, "inproc://test");
 assert(ret == 0); zmq_pollitem_t pollItem;
 pollItem.socket = socket;
 pollItem.events = 0;
 pollItem.fd = 0;
 pollItem.revents = ZMQ_POLLIN; while (1)
 {
  cout << "polling..." << endl;
  zmq_poll(&pollItem, 1, 1000);  if (pollItem.revents & ZMQ_POLLIN)
  {
   zmq_msg_t message;
   zmq_msg_init(&message);
   zmq_recvmsg(pollItem.socket, &message, ZMQ_DONTWAIT);
   std::string* s = new std::string((char*)zmq_msg_data(&message), zmq_msg_size(&message));
   zmq_msg_close(&message);
   cout << "s = '" << s << "'" << endl;
  }
 }
} int main()
{
 void* context = zmq_init(1);
 void* socket = zmq_socket(context, ZMQ_PUB);
 zmq_bind(socket, "inproc://test"); cout << "STARTING THREAD" << endl;
 pthread_t testThread;
 pthread_create(&testThread, NULL, startTestThread, context); sleep(5); cout << "Sending message...";
 string s = "test";
 zmq_msg_t msg;
 zmq_msg_init_size(&msg, s.length());
 memcpy(zmq_msg_data(&msg), s.c_str(), s.length());
 zmq_sendmsg(socket, &msg, ZMQ_DONTWAIT);
 zmq_msg_close(&msg);
 cout << "done" << endl; while (true);
} 		 	   		  
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.zeromq.org/pipermail/zeromq-dev/attachments/20120824/f6edad6f/attachment.htm>


More information about the zeromq-dev mailing list