[zeromq-dev] how to make the hello world example can not work[zeromq 2.1.7]?
Pieter Hintjens
ph at imatix.com
Tue Jul 19 13:21:23 CEST 2011
Hi bbxiong,
The problem is that you copied the client code but lost one line:
std::cout << "Sending Hello " << request_nbr << "..." << std::endl;
>>> socket.send (request);
If you try to recv on a REQ socket, without first sending a message,
you get the exception you hit.
-Pieter
On Tue, Jul 19, 2011 at 1:05 PM, bbxiong <bbxiong.xiao at gmail.com> wrote:
> 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!
> _______________________________________________
> zeromq-dev mailing list
> zeromq-dev at lists.zeromq.org
> http://lists.zeromq.org/mailman/listinfo/zeromq-dev
>
>
More information about the zeromq-dev
mailing list