title: FD_ISSET returns always false
body: <p>I'm new in C sockets programming, and I'm trying to implement client-server non-blocking application, by using select(...). </p>
  
  <p>When I run on debug the server code and try to connect the client, the select(...) returns 1 (as expected) but for some reason, the FD_ISSET doesn't find the file descriptor ID in the descriptors set.</p>
  
  <p>I've already spend several days in debugging and I'm running out of ideas what could be the problem. 
  Any help would be much appreciated!</p>
  
  <pre><code>void server_listen(int port_number){
  
  
      tcpsock_t * server, * client;
      fd_set master_set, working_set;
      int listen_sd;
  
      int max_sd, rc, desc_ready, bytes, result, close_conn;
      dplist_node_t * reference;
  
  
  
  
      if (tcp_passive_open(&amp;server,PORT)!=TCP_NO_ERROR) exit(EXIT_FAILURE);
      if (tcp_get_sd(server,&amp;listen_sd)!=TCP_NO_ERROR) exit(EXIT_FAILURE);
  
      FD_ZERO(&amp;master_set);
      max_sd = listen_sd;
      FD_SET(listen_sd, &amp;master_set);
  
      do {
          timeout.tv_sec  = 30;
          timeout.tv_usec = 0;
  
          memcpy(&amp;working_set, &amp;master_set, sizeof(master_set));
          printf("Waiting on select()...\n");
          rc = select(max_sd + 1, &amp;working_set, NULL, NULL, &amp;timeout);
  
          if (rc &lt; 0)
          {
              perror("  select() failed");
              break;
          }
          if (rc == 0)
          {
              printf("  select() timed out.\n");
  
          }
          desc_ready = rc;
          for (int i=1; i &lt;= max_sd  &amp;&amp;  desc_ready &gt; 0; ++i)
          {
              if (FD_ISSET(i, &amp;working_set))
              {
                  desc_ready -= 1;
  
                  if (i == listen_sd)
                  {
                      printf("  Listening socket is readable\n");
  
                    //do something with the new socket
                  }
  
              }
              else
              {
                  printf("  Descriptor %d is readable\n", i);
                  close_conn = FALSE;
  
                 //read data from already existing socket connection
  
              } /* End of existing connection is readable */
          } /* End of if (FD_ISSET(i, &amp;working_set)) */
      }
      while (end_server == FALSE);
  
  }
  
  int tcp_passive_open(tcpsock_t ** sock, int port)
  {
      int result;
      struct sockaddr_in addr;
  
      tcpsock_t * s = (tcpsock_t *) malloc(sizeof(tcpsock_t));
      s-&gt;cookie = 0;  
      s-&gt;port = -1;
      s-&gt;ip_addr = NULL;
      s-&gt;sd = -1;
  
      s-&gt;sd = socket(PROTOCOLFAMILY, TYPE, PROTOCOL);
  
      memset(&amp;addr, 0, sizeof(struct sockaddr_in));
      addr.sin_family = PROTOCOLFAMILY;
      addr.sin_addr.s_addr = htonl(INADDR_ANY);
      addr.sin_port = htons(port);
      result = bind(s-&gt;sd,(struct sockaddr *)&amp;addr,sizeof(addr));
      result = listen(s-&gt;sd,MAX_PENDING);
      s-&gt;ip_addr = NULL; 
      s-&gt;port = port;
      s-&gt;cookie = MAGIC_COOKIE;
      *sock = s;
      return TCP_NO_ERROR;
  }
  
  int tcp_get_sd(tcpsock_t * socket, int * sd)
  {
    *sd = socket-&gt;sd;
    return TCP_NO_ERROR;
  }
  </code></pre>