[zeromq-dev] Unable to receive reply at Java client from C++ server

Deepak Jharodia deepakjharodia at gmail.com
Wed Feb 13 07:42:40 CET 2013


I'm trying my first ZMQ example. I have been able to solve some off the
problems. The client is Java and server is C++. I've been able to
successfully send message from client to server, but the reply doesn't
reach back to client.


Server code:
 while (true) {
      zmq::message_t request;

      //  Wait for next request from client
      socket.recv (&request);
      std::cout << "Received Hello" << std::endl;

      //  Do some 'work'
      sleep (1);

      //  Send reply back to client
      zmq::message_t reply (5);
      memcpy ((void *) reply.data (), "World", 5);
      std::cout << "Sending world" << std::endl;
      socket.send (reply);
   }


Client code:
for (int request_nbr = 0; request_nbr != 10; request_nbr++) {
                // Create a "Hello" message.
                // Ensure that the last byte of our "Hello" message is 0
because
                // our "Hello World" server is expecting a 0-terminated
string:
                String requestString = "Hello" + " ";
                byte[] request = requestString.getBytes();
                request[request.length - 1] = 0; // Sets the last byte to 0
                // Send the message
                System.out.println("Sending request " + request_nbr +
"\u2026");
                socket.send(request, 0);

                System.out.println("Waiting for reply " + request_nbr +
"\u2026");

                // Get the reply.
                byte[] reply = socket.recv(0);
                // When displaying reply as a String, omit the last byte
because
                // our "Hello World" server has sent us a 0-terminated
string:
                System.out.println("Received reply " + request_nbr + ": ["
                        + new String(reply, 0, reply.length - 1) + "]");
            }

Output:
Bound. Waiting for client..
Received Hello
Sending world
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.zeromq.org/pipermail/zeromq-dev/attachments/20130213/e2af5cf5/attachment.html>


More information about the zeromq-dev mailing list