[zeromq-dev] libev REQ/REP problem

Robert G. Jakabosky bobby at sharedrealm.com
Tue Mar 29 07:17:34 CEST 2011

On Monday 28, jrt at hodgepig.org wrote:
> Hi,
> I'm having problems combining libev with 0MQ to act on a REP socket
> over TCP.
> My code is here: http://www.hodgepig.org/testcase/
> Tarball here: http://www.hodgepig.org/testcase/testcase.tar.gz
> I have written a simple blocking client which sends strings from the
> user and a simple blocking server to respond. These work fine.
> http://www.hodgepig.org/testcase/client.c
> http://www.hodgepig.org/testcase/server.c
> However, when I add a libev event loop to the server (
> http://www.hodgepig.org/testcase/evserver.c ) I see problems. The libev
> server receives the initial packet, and responds but tcpdump never shows
> the response being transmitted (even though zmq_send() is returning 0).
> Can anyone explain what's going on?

You should only register for EV_READ events on the 0MQ socket's FD, since it 
is only a controls socket (unix domain socket).  The 0MQ socket's FD is edge-
triggered so you need to consume all ZMQ_EVENTS from the socket when you get a 
EV_READ event from libev, this means you need to call zmq_getsockopt(sock, 
ZMQ_EVENTS, &events, sizeof(events)) until "events" reports 0 events.  So you 
either need to do that in the IO watcher or you need to use an idle watcher 
(i.e. enable it when the fd was reported as readable, then disable it when 
ZMQ_EVENTS reports no 0MQ events).  The reason for this is that you will not 
keep getting a EV_READ event for the socket's FD.  Normal TCP sockets are 
level-triggered (i.e. you keep getting a READ event until the input buffer is 
empty, or a WRITE event until the buffer is full).

I use the idle watcher method, so one socket can't starve all the other 
sockets for work.  The idle watcher is limited to processing X number of 
read/write events.

Attached is a fixed version of your evserver.c example.  Your version was also 
consuming all the cpu, since it was always getting a EV_WRITE event for the 
0MQ socket's FD.

Robert G. Jakabosky
-------------- next part --------------
A non-text attachment was scrubbed...
Name: evserver.c
Type: text/x-csrc
Size: 3225 bytes
Desc: not available
URL: <https://lists.zeromq.org/pipermail/zeromq-dev/attachments/20110328/5842d33f/attachment.c>

More information about the zeromq-dev mailing list