title: Client/Server Programming for Allowing Multiple Clients
body: <p>So currently I am trying to write a code where the client sends a code and a number to the server where the server will use that code to figure out which conversion it needs to make for the number. As of now I have the whole program written up but only for one client. How do I make it to where it accepts up to 10 clients?</p>
  
  <pre><code>/* convserver2.cc
   *
   * Created by: Aaron Eiser
   * 2/26/2020
   */
  
  #include "socket.h"
  #include "fdset.h"
  #include &lt;string.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;sys/time.h&gt;
  #include &lt;ctype.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;signal.h&gt;
  
  void ctrlCHandler(int signal);
  float conversion(unsigned char code, float value);
  bool isValid(unsigned char code);
  
  const unsigned char CONV_CODES[] = {0, 11, 12, 13, 14, 15, 16, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32};
  const int LENGTH = 19;
  const int MAX_CLIENTS = 10;
  int clientSock[10];
  int master_socket, new_socket, activity, valread, sd, max_sd;
  ServerSocket theServer;
  Socket *theClient = NULL;
  
  // struct for packet
  struct Packet{
      float value;
      char gap[3];
      unsigned char code;
  };
  
  
  
  int main(int argc, char *argv[])
  {
      fd_sett readfds;
  
      int port = atoi(argv[1]);
  
      for(i = 0; i &lt; MAX_CLIENTS; i++){
          clientSock[i] = 0;
      }
  
  
      /* Bind the server listening socket to the given port. */
    theServer.bind(port);    
    printf("Server bound to port #%d\n", port);
  
    Packet data;
  
    bool flags = true;
    while(flags){
        signal(SIGINT, ctrlCHandler);
  
        theClient = theServer.accept();
        printf("Connection made\n");
  
        int size = theClient -&gt; recv(&amp;data, sizeof(Packet));
        printf("Packet receieved\n");
  
        while(size &gt; 0 and data.code != 0){
            Packet newData;
            if(!isValid(data.code)){
                fprintf(stderr, "Invalid code\n");
                newData.code = 0;
                newData.value = conversion(data.code, data.value);
                theClient -&gt; send(&amp;newData, sizeof(Packet));
                printf("Packet sent\n");
                size = theClient -&gt; recv(&amp;data, sizeof(Packet));
                printf("Packet received\n");
            }
            else{
                newData.code = data.code;
                newData.value = conversion(data.code, data.value);
                theClient -&gt; send(&amp;newData, sizeof(Packet));
                printf("%d %f \n", newData.code, newData.value); 
                printf("Packet sent\n");
                size = theClient -&gt; recv(&amp;data, sizeof(Packet));
                printf("Packet received\n");
            }
        }
        theClient -&gt; close();
        delete theClient;
    }
  }
  
  float conversion(unsigned char code, float value){
      float newValue;
      // Conv code 11
      if(code == 11){
          newValue = (value - 32) * (5.0 / 9.0);
          printf("something\n");
      }
      // Conv code 12
      else if(code == 12){
          newValue = (value - 32) * (5.0 / 9.0) + 273.15;
      }
      // Conv code 13
      else if(code == 13){
          newValue = (value * (9.0 / 5.0)) + 32;
      }
      // Conv code 14
      else if(code == 14){
          newValue = value + 273.15;
      }
      // Conv code 15
      else if(code == 15){
          newValue = (value - 273.15) * (9.0 / 5.0) + 32;
      }
      // Conv code 16
      else if(code == 16){
          newValue = value - 273.15;
      }
      // Conv code 21
      else if(code == 21){
          newValue = value / 12.0;
      }
      // Conv code 22
      else if(code == 22){
          newValue = value / 36.0;
      }
      // Conv code 23
      else if(code == 23){
          newValue = value / 63360.0;
      }
      // Conv code 24
      else if(code == 24){
          newValue = value * 12.0;
      }
      // Conv code 25
      else if(code == 25){
          newValue = value / 3.0;
      }
      // Conv code 26
      else if(code == 26){
          newValue = value / 5280.0;
      }
      // Conv code 27
      else if(code == 27){
          newValue = value * 36.0;
      }
      // Conv code 28
      else if(code == 28){
          newValue = value * 3.0;
      }
      // Conv code 29
      else if(code == 29){
          newValue = value / 1760.0;
      }
      // Conv code 30
      else if(code == 30){
          newValue = value * 63360.0;
      }
      // Conv code 31
      else if(code == 31){
          newValue = value * 5280.0;
      }
      // Conv code 32
      else if(code == 32){
          newValue = value * 1760.0;
      }
      return newValue;
  }
  
  // Decides whether code is valid
  bool isValid(unsigned char code){
      for(int i = 0; i &lt; LENGTH; i++){
          if(code == CONV_CODES[i]){
              return true;
          }
      }
      return false;
  }
  
  // Catches Ctrl C
  void ctrlCHandler(int signal){
      if(theClient != NULL){
          theClient -&gt; close();
      }
      theServer.close();
      exit(0);
  }
  </code></pre>