title: poll(2) does not return when there is data ready
body: <p>I am attempting to write a socket abstraction in C++. I am using the poll() system call to wait for data to become ready for reading. However, when I run my program, poll() never returns if I have it set to indefinitely poll, even if I send the socket data.</p>
  
  <p>Here is a simple program to illustrate my problem. I am compiling it on OSX with this line: <code>clang++ -g -Wall -pedantic -std=c++0x -stdlib=libc++ pollTest.cpp -o pollTest</code>. I am using the <code>nc</code> program to set up a listening socket for the program to connect to. When I run this, the poll() call hangs forever, never returning due to data being ready to read.</p>
  
  <pre><code>#include &lt;poll.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;sys/socket.h&gt;
  #include &lt;netinet/tcp.h&gt;
  #include &lt;netdb.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;poll.h&gt;
  #include &lt;fcntl.h&gt;
  #include &lt;cstdlib&gt;
  #include &lt;iostream&gt;
  
  void connectSocket(int&amp; fd, std::string hostname, std::string service) {
    struct addrinfo hints, *res, *res0;
    int error;
    int s;
    memset(&amp;hints, 0, sizeof(hints));
    hints.ai_family = AF_INET;
    hints.ai_socktype = SOCK_STREAM;
    error = getaddrinfo(hostname.c_str(), service.c_str(), &amp;hints, &amp;res0);
    if (error) {
      throw error;
    }
    s = -1;
    for (res = res0; res; res = res-&gt;ai_next) {
      s = socket(res-&gt;ai_family, res-&gt;ai_socktype,
                  res-&gt;ai_protocol);
      if (s &lt; 0) {
        continue;
      }
  
      if (connect(s, res-&gt;ai_addr, res-&gt;ai_addrlen) &lt; 0) {
        close(s);
        s = -1;
        continue;
      }
  
      break;  /* okay we got one */
    }
  
    if (s &lt; 0) {
      throw s;
    }
  }
  
  int main() {
    int socketFileDescriptor = -1;
      connectSocket(socketFileDescriptor, "localhost", "9999");
  
    pollfd socketPolls[1];
    socketPolls[0].fd = socketFileDescriptor;
    socketPolls[0].events = POLLIN | POLLPRI | POLLRDBAND | POLLRDNORM;
    poll(socketPolls, 1, -1);
    std::cerr &lt;&lt; socketPolls[0].revents;
  }
  </code></pre>
  
  <p>Any ideas on why this is happening? I feel as if I have read the documentation well, and am using the system calls correctly. (I didn't do error checking in this example program, but I do in my project). Any help would be appreciated!</p>