[zeromq-dev] how to make the hello world example can not work[zeromq 2.1.7]?

bbxiong bbxiong.xiao at gmail.com
Tue Jul 19 13:05:36 CEST 2011


Hi, listers,

  I installed zeromq 2.1.7 from source, when i'm trying to run the basic
hello world example on my 64bits-Suse(using c++ api),
i always get an exception zmq::error_t:
  Operation cannot be accomplished in current state

  I'm confused about the error message, seems it crashes when recving the
World response in client side.

  Below is the code sample, just copied from the guide, the client crashed
at "socket.recv(&reply);" this line:

  [server]:
#include <zmq.hpp>
#include <string>
#include <iostream>
#include <unistd.h>

int main()
{
  zmq::context_t context(1);
  zmq::socket_t socket(context, ZMQ_REP);
  socket.bind("tcp://*:5555");

  while (true)
  {
    zmq::message_t request;
    socket.recv(&request);
    std::cout << "Received Hello " << std::endl;

    sleep(1);

    zmq::message_t reply(5);
    memcpy((void *)reply.data(), "World", 5);
    socket.send(reply);
  }

  return 0;
}


[client]:
#include <zmq.hpp>
#include <string>
#include <iostream>
#include <unistd.h>

int main()
{
  zmq::context_t context(1);
  zmq::socket_t socket(context, ZMQ_REQ);

  std::cout << "Connecting to hello world server" << std::endl;
  socket.connect("tcp://localhost:5555");

  try
  {
    for (int request_nbr = 0; request_nbr != 10; request_nbr++)
    {
      zmq::message_t request(6);
      memcpy((void *)request.data(), "Hello", 5);
      std::cout << "Sending Hello " << request_nbr << "..." << std::endl;

      zmq::message_t reply;
      socket.recv(&reply);

      std::cout << "Received World " << request_nbr << std::endl;
    }
  }
  catch (std::exception &e)
  {
    std::cerr << "Exception occurred, " << e.what() << std::endl;
  }

  return 0;
}


--
Best!
bbxiong!
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.zeromq.org/pipermail/zeromq-dev/attachments/20110719/8dfef403/attachment.htm>


More information about the zeromq-dev mailing list