title: What causes error in the poll() function, c++?
body: <p>I'm fairly new to socket programming, and I'm trying to write a program that would get incoming tcp connections and manage them somehow. I can't figure out why the following code is giving me a "poll error":</p>
  
  <pre><code>int main(int argc, char *argv[]) {
  char *port;
  struct pollfd connections[MAX_CONNECTIONS];
  struct addrinfo addr_hints, *addr_result;
  int ret, i;
  
  for (i = 0; i &lt; MAX_CONNECTIONS; ++i) {
      connections[i].fd = -1;
      connections[i].events = POLLIN;
      connections[i].revents = 0;
  }
  
  port = "0";
  
  memset(&amp;addr_hints, 0, sizeof(struct addrinfo));
  addr_hints.ai_flags = AI_PASSIVE;
  addr_hints.ai_family = AF_UNSPEC;
  addr_hints.ai_socktype = SOCK_STREAM;
  addr_hints.ai_protocol = IPPROTO_TCP;
  
  getaddrinfo(NULL, port, &amp;addr_hints, &amp;addr_result);
  
  connections[0].fd = socket(addr_result-&gt;ai_family, addr_result-&gt;ai_socktype, addr_result-&gt;ai_protocol);
  
  if (connections[0].fd &lt; 0) {
      cerr &lt;&lt; "Socket error" &lt;&lt; endl;
      return 0;
  }
  
  if (bind(connections[0].fd, addr_result-&gt;ai_addr, addr_result-&gt;ai_addrlen) &lt; 0) {
      cerr &lt;&lt; "Bind errror" &lt;&lt; endl;
      return 0;
  }
  
  if (listen(connections[0].fd, 25) &lt; 0) {
          cerr &lt;&lt; "Listen error" &lt;&lt; endl;
      return 0;
  }
  
  do {
      for (i = 0; i &lt; MAX_CONNECTIONS; ++i)
              connections[i].revents = 0;
  
      ret = poll(connections, MAX_CONNECTIONS, -1);
  
      if (ret &lt; 0) {
          cerr &lt;&lt; "Poll error" &lt;&lt; endl;
          return 0;
      } else {
          //DO SOMETHING
      }
  
  } while(true);
  }
  </code></pre>
  
  <p>MAX_CONNECTIONS is a constant set to 10000. Connections[0] is supposed to be the descriptor on which i am listening to incoming connections. I set port to "0" because i want to pick a random port. It seems that the poll function fails immediately, yielding the message "Poll error" (so poll() was less than 0 basically). I've checked and after poll and bind connections[0] has a file descriptor. I'm not sure what I'm doing wrong, is it something with the getaddrinfo function?</p>