私は最初の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