Issue with reading data on socket

Pierre-Yves Kerembellec py.kerembellec at
Thu Oct 13 08:50:08 CEST 2011

Le 13 oct. 2011 à 08:12, sai koushik a écrit :

> Hi ,
> i tried to crate a socket client using  libev , and this client has to listen for data from the server.
> and i tried as below
>         ev_io stdin_watcher;
>         ....
>         sock_fd = socket(PF_UNIX, SOCK_STREAM, 0);
>         memset(&addr, 0, sizeof(addr));
>         addr.sun_family = AF_UNIX;
>         strcpy(addr.sun_path, ifname);
>         err = connect(sock_fd, (struct sockaddr *) &addr, sizeof(addr));
>         int flags=0;
>         fcntl(sock_fd, F_SETFL, flags | O_NONBLOCK);
>         ev_io_init(&stdin_watcher,stdin_cb, sock_fd, EV_READ);
>         ev_io_start (EV_DEFAULT_UC_ &stdin_watcher);
>         ....
> and callback is 
>     static void stdin_cb (EV_P_ ev_io *w, int revents)
>     {
>         printf("\n callback \n");
>     }
> issue is when i tried to run client program callback is getting invoked forever and it is going into infinte loop.
> i didn`t used ev_loop.
> Is above code is in proper way of reading data from from server?
> any help?

- you should use ev_loop() since it's the heart of libev for calling callbacks (not using kind of defeat the whole libev purpose).

- you should do something more than a printf in you callback (i.e. actually reading from the socket) since events in libev are
  level-trigerred (and not edge-trigerred) : the callback will get invoked forever in a tight loop until the condition that triggered
  it int the first place is de-asserted (in your case, EV_READ, i.e. "some data (or special condition) is waiting to be read from
  the observed descriptor").

Have a look here for more info on this :

Hope this helps,

More information about the libev mailing list