无法从C ++服务器接收Java客户端的回复

时间:2013-02-12 10:37:45

标签: java c++ zeromq

我正在尝试我的第一个ZMQ示例。客户端是Java,服务器是C ++。我已经能够成功地将消息从客户端发送到服务器,但是回复没有回到客户端。

服务器代码:

 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);
   }

客户代码:

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) + "]");
            }

输出:

Bound. Waiting for client..
Received Hello
Sending world

0 个答案:

没有答案