Example unix socket echo server

Marc Lehmann schmorp at schmorp.de
Tue Nov 2 08:41:13 CET 2010

On Mon, Nov 01, 2010 at 10:44:58PM -0600, AJ ONeal <coolaj86 at gmail.com> wrote:
> The question that I have is if it's true to say that a unix socket is
> writable whenever it is readable?

No, a unix domain socket works mostly like other stream sockets.

> When I tried listening for when it was writable, it seemed to fire the event
> over and over again.

Do you have reason to assume that it was not writable over and over again?

For it not to be writable you'd have to write enough data to fill it's
buffers, and make sure nobody is reading on the other side.

> Or is it that when I want to send data I should
>     start the watcher for writes
>     send the data
>     stop the watcher
> ?

If you mean your write watcher was always active, then this explains your
issue. Think about it, you ask libev when the socket is writable, but
don't write anything. This means the socket will stay writable - there is
no way for libev to know thta for some reason you are no longer interested
in writability, except by stopping the watcher.

A common (not necessarily most efficient) way to handle writes is:

   start the watcher
   in callback
      write more of the remaining data
      if all data has been written, stop the watcher

                The choice of a       Deliantra, the free code+content MORPG
      -----==-     _GNU_              http://www.deliantra.net
      ----==-- _       generation
      ---==---(_)__  __ ____  __      Marc Lehmann
      --==---/ / _ \/ // /\ \/ /      schmorp at schmorp.de
      -=====/_/_//_/\_,_/ /_/\_\

More information about the libev mailing list