title: Socket programming in C, using the select() function
body: <p>Based from the answers I got from <a href="https://stackoverflow.com/q/3366808">this thread</a>, I've created this:</p>
  
  <pre><code>    //Server 
  
      sock_init(); //from SFL, see http://legacy.imatix.com/html/sfl/
  
      timeout = 50000;
  
      serv_sock_input[0] = TCP(1234); 
      serv_sock_input[1] = UDP(9876);
  
      input_protocols[0] = "tcp";
      input_protocols[1] = "udp";
  
      while (1)
      {
          FD_ZERO(&amp;sock_set);
          for (x = 0; x&lt;number_of_inputs; x++)
          {
              FD_SET(serv_sock_input[x], &amp;sock_set);
          }
  
          select_timeout.tv_sec = timeout;
          select_timeout.tv_usec = 0;
  
          if (select(0, &amp;sock_set, NULL, NULL, &amp;select_timeout) == 0)
              printf("No requests");
          else
          {
              for (x = 0; x&lt;number_of_inputs; x++)
              {
                  if (FD_ISSET(serv_sock_input[x],&amp;sock_set))
                  {
                      printf("\nRequest on port %d: \n", x);
                      if ((strcmp(input_protocols[x],"tcp")) == 0) //in this case, 0 returned == TRUE
                      {
                          accept_socket(serv_sock_input[x]);
                          printf("Input TCP Port %d\n",x);
                          close_socket(serv_sock_input[x]);
                      }
                      else
                      {
                          printf("Input UDP Port %d\n",x);
                      }
                  }
              }
          }
      }
      sock_term();
  }
  </code></pre>
  
  <hr>
  
  <pre><code>int TCP (unsigned short port)
  {
      int sock;                        
      struct sockaddr_in servAddr; 
  
      if ((sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) &lt; 0)
          exit(1);
  
      memset(&amp;servAddr, 0, sizeof(servAddr));
      servAddr.sin_family = AF_INET;         
      servAddr.sin_addr.s_addr = htonl(INADDR_ANY);
      servAddr.sin_port = htons(port);            
  
      if (bind(sock, (struct sockaddr *) &amp;servAddr, sizeof(servAddr)) &lt; 0)
          exit(1);
  
      if (listen(sock, 5) &lt; 0)
          exit(1);
  
      return sock;
  }
  </code></pre>
  
  <hr>
  
  <pre><code>int UDP (unsigned short port)
  {
      int sock;                        /* socket to create */
      struct sockaddr_in servAddr; /* Local address */
  
      /* Create socket for sending/receiving datagrams */
      if ((sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) &lt; 0)
          exit(1);
  
      /* Construct local address structure */
      memset(&amp;servAddr, 0, sizeof(servAddr));   /* Zero out structure */
      servAddr.sin_family = AF_INET;                /* Internet address family */
      servAddr.sin_addr.s_addr = htonl(INADDR_ANY); /* Any incoming interface */
      servAddr.sin_port = htons(port);      /* Local port */
  
      /* Bind to the local address */
      if (bind(sock, (struct sockaddr *) &amp;servAddr, sizeof(servAddr)) &lt; 0)
          exit(1);
  
  
      return sock;
  }
  </code></pre>
  
  <hr>
  
  <pre><code>//Client
  sock_init();
  
      if ((client_sock_output = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) &lt; 0)
          exit(1);
  
      memset(&amp;client_addr, 0, sizeof(client_addr));
      client_addr.sin_family      = AF_INET;
      client_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
      client_addr.sin_port        = htons(1234);
  
      if (connect(client_sock_output, (struct sockaddr *) &amp;client_addr, sizeof(client_addr)) &lt; 0)
          exit(1);
  
      closesocket(client_sock_output);
  
      sock_term();
  </code></pre>
  
  <p>When the server starts, the server gets blocked at the <code>if(select(...))</code> statement.</p>
  
  <p>So when I run the Server, and then the client, the client connects to the server (sometimes it takes a couple times to run the client before they connect). Then the <code>if(select...))</code> statement is no longer true and it proceeds to the else.</p>
  
  <p>After that, the client closes the connection, and the program. However, and this is where my problem happens, the <code>if(select(...))</code> statement is always false. I get this output:  </p>
  
  <pre><code>Request on port 0:  
  Input TCP Port 0  
  
  Request on port 1:  
  Input UDP Port 1
  </code></pre>
  
  <p>This output repeats forever. How come it doesn't get stuck at the <code>if(select(...))</code>?</p>