Re: poll() returns POLLIN but no data ?

From: David Schwartz (davids_at_webmaster.com)
Date: 06/25/05


Date: Sat, 25 Jun 2005 13:27:02 -0700


"Leon Mergen" <lmergen@gmail.com> wrote in message
news:1119705877.8010.10.camel@localhost.localdomain...
> Hello,
>
> Consider this piece of code:
>
> ---
> struct pollfd ufds;
> char buffer[8192] = "";
>
> ufds.fd = sockStream->getHandle ();
> ufds.events = POLLIN|POLLHUP;
> ufds.revents = 0;
>
> int result = poll ( &ufds, 1, 30000 );
> if (result > 0) {
> if (ufds.revents & POLLHUP ) {
> // Close connection
> } else if (ufds.revents & POLLIN ) {
> recv (sockStream->getHandle, buffer, sizeof(buffer), 0);
> }
> }
> ---
>
> For some reason, ufds.revents matches POLLIN, but when executing the
> blocking recv function, there is no data and that function blocks. I'm
> running Linux 2.6.10, and have /no/ idea what might be causing this.
> Anyone else has an idea ?

    You should be using non-blocking sockets if you don't want to block. The
system does not "remember" that it gave you the POLLIN indication and
magically convert the "next" operation to a non-blocking one. Also, you are
ignoring the return value of 'recv'.

    DS