title: C - Using poll to multiplex between socket(s) and stdin - Server
body: <p>I'm writing a client server application and I'm using poll to multiplex between several client sockets and stdin, where I can insert commands (example: stop the server). I believe the structure  (the "logic") of my code is correct, however it's not behaving the way I expect it to:</p>
  
  <pre><code>struct pollfd pfd[NSERVER]; //defined as 10
  pfd[0].fd = fileno(stdin);
  pfd[0].events = POLLIN;
  pfd[1].fd = socktfd; //server bind, listen socket
  pfd[1].events = POLLIN;
  struct sockaddr_storage remoteaddr; // client address
  socklen_t addrlen;
  char remoteIP[INET6_ADDRSTRLEN];
  addrlen = sizeof remoteaddr;
  char buf[1024];     // buffer
  int pos=2;  
  
  while(poll(pfd,1,0) &gt;= 0)
  {
      if(pfd[0].revents &amp; POLLIN) { //stdin
              //process input and perform command
          }
      if(pfd[1].revents &amp; POLLIN) {
          /* new connection */
          int connsockfd = accept(socktfd, (struct sockaddr *)&amp;remoteaddr,&amp;addrlen);  
          pfd[pos].fd=connsockfd;
      }
      int i=2;
      //Loop through the fd in pfd for events
      while (i&lt;=NSERVER)
      {
          if (pfd[i].revents &amp; POLLIN) {
              int c=recv(pfd[i].fd, buf, sizeof buf, 0);
              if(c&lt;=0) {
                  if (c==0)
                  {
                  /* Client closed socket */
                      close(pfd[i].fd);
                  }
              }else
              {//Client sent some data
                  c=send(pfd[i].fd,sbuff,z,0);
                  if (c&lt;=0)
                  {
                      Error;
                  }
              free(sbuff);
              }
          }
          i++;
      }
  }
  </code></pre>
  
  <p>I've removed some code inside the recv and send to make the code easier to read.
  It fails to behave (it just hangs, doesn't accept connections or reacts to input from stdin).</p>
  
  <p>Note: I would prefer to use poll over select, so please don't point to select :-).</p>
  
  <p>Thanks in advance for any assistance.</p>