I slightly modified the helloworld server program (hwserver.pl) from the ZeroMQ guide, to implement it with AnyEvent. Yet after two iterations of REQ/REP, the program hangs. Can someone figure out why?
Here's the server:
#!/usr/bin/perl -w
use strict;
use warnings;
use 5.12.0;
use EV;
use AnyEvent;
use ZMQ::LibZMQ3;
use ZMQ::Constants qw/ ZMQ_REP ZMQ_FD /;
my $context = zmq_init();
my $responder = zmq_socket($context, ZMQ_REP);
my $fh = zmq_getsockopt( $responder, ZMQ_FD );
zmq_bind($responder, 'tcp://*:5555');
our $w; $w = AE::io $fh, 0, sub {
say "Receiving...";
zmq_recv($responder, my $buf, 1_000_000);
say "Received request: [$buf]";
sleep(1);
zmq_msg_send('World', $responder);
sleep(1);
};
EV::run;
And here's the client:
#!/usr/bin/perl -w
use strict;
use warnings;
use 5.12.0;
use ZMQ::LibZMQ3;
use ZMQ::Constants qw(ZMQ_REQ);
my $context = zmq_init();
# Socket to talk to server
say 'Connecting to hello world server...';
my $requester = zmq_socket($context, ZMQ_REQ);
zmq_connect($requester, 'tcp://localhost:5555');
for my $request_nbr (0..9) {
say "Sending request $request_nbr...";
zmq_msg_send('Hello', $requester);
my $msg = zmq_msg_init();
say "Receiving...";
zmq_msg_recv($msg, $requester);
say "Received reply $request_nbr: [". zmq_msg_data($msg) ."]";
}
And here's the output of the server:
Receiving...
Received request: [Hello]
Receiving...
Received request: [Hello]
And here's the output of the client:
Connecting to hello world server...
Sending request 0...
Receiving...
Received reply 0: [World]
Sending request 1...
Receiving...
Received reply 1: [World]
Sending request 2...
Receiving...
What's wrong?
Is there a reason you didn't use the while() syntax the docs suggest for your server?
http://search.cpan.org/~dmaki/ZMQ-LibZMQ3-1.00_04/lib/ZMQ/LibZMQ3.pm
Also, a sleep is going to be blocking. You should really register a callback tied to a timer event set to run after a second that will send the response.