title: Improve a server with select() function
body: <p>I read about select() and also read many examples of it, but I can't understand when can I use it?
  I understood that I can use it in the accept() function, in case I want few people to connect to the server, but it has confused me.
  I need to built a server that receive data only from 2 clients, 1 every time. The first user sends to the server a string, the string responds and then the second user sends a string.
  Can someone help me with combining the select() function in the recv() function?
  I've added my server.cpp code. Thank you!</p>
  
  <p>server:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;winsock2.h&gt;
  #include &lt;string&gt;
  #include &lt;windows.h&gt;
  #include &lt;vector&gt;
  #pragma comment(lib,"ws2_32.lib")
  
  #define MAX_NUMBER_OF_PLAYERS 1
  #define BUFFER_SIZE 1024
  #define LIMIT 1
  
  // server side 
  #define INVALID_MOVE 00
  #define PLEASE_ENTER_A_MOVE 15
  #define PRINT_BOARD 20
  #define END_GAME 30
  
  // client side
  #define MOVE 10
  
  
  using namespace std;
  
  int main()
  {
      WSADATA WsaDat;
      SOCKET clientsock[2];
      int minsock = 0;
      int numsocks = MAX_NUMBER_OF_PLAYERS;
  
      if (WSAStartup(MAKEWORD(2, 2), &amp;WsaDat) != 0)
      {
          std::cout &lt;&lt; "WSA Initialization failed!\r\n";
          WSACleanup();
          system("PAUSE");
          return 0;
      }
  
      SOCKET serverSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
      if (serverSocket == INVALID_SOCKET)
      {
          std::cout &lt;&lt; "Socket creation failed.\r\n";
          WSACleanup();
          system("PAUSE");
          return 0;
      }
  
      SOCKADDR_IN serverInf;
      serverInf.sin_family = AF_INET;
      serverInf.sin_addr.s_addr = INADDR_ANY;
      serverInf.sin_port = htons(8888);
  
      if (bind(serverSocket, (SOCKADDR*)(&amp;serverInf), sizeof(serverInf)) == SOCKET_ERROR)
      {
          std::cout &lt;&lt; "Unable to bind socket!\r\n";
          WSACleanup();
          system("PAUSE");
          return 0;
      }
  
      listen(serverSocket, 5);
  
  
  
      clientsock[0] = accept(serverSocket, NULL, NULL);
      cout &lt;&lt; "Client 1 has connected." &lt;&lt; endl;
      clientsock[1] = accept(serverSocket, NULL, NULL);
      cout &lt;&lt; "Client 2 has connected." &lt;&lt; endl;
  
      for (int i = 0; i &lt; 2; i++)
      {
          cout &lt;&lt; "Client " &lt;&lt; i+1  &lt;&lt; " Has Connected!" &lt;&lt; endl;
      }
  
      char client1_buffer[BUFFER_SIZE];
      char client2_buffer[BUFFER_SIZE];
      char* clientBuffer;
      // until there isn't a mate.
      bool gameRunning = true;
      // user represents if it's user1 (0), or user2(1)
      bool user = 0;
  
  
      while (gameRunning)
      {
          if (!user)
              clientBuffer = client1_buffer;
          else
              clientBuffer = client2_buffer;
  
          int in = recv(clientsock[user], clientBuffer, BUFFER_SIZE, 0);
          cout &lt;&lt; in &lt;&lt; endl;
          if (in &gt; 0)
          {
                  // CHECKS
                  // MOVE COMMAND
                  // IF worked, send the board to both clients. if current user = 1 ==&gt; do user to 0 | if the user = 0 =&gt; do user to 11
                  // ELSE, send the current client (clientsock[user]) Error message and ask for a command again.
                  cout &lt;&lt; clientBuffer &lt;&lt; endl;
                  cout &lt;&lt; " IN RECV";
                  char* szMessage = "15";
                  send(clientsock[user], szMessage, sizeof(szMessage), 0);
          }
          else if (in == 0)
          {
              // The connection has closed.
              // REMEMBER : SAVE THE GAME SITUATION.
          }
          else
          {
              printf("recv failed: %d\n", WSAGetLastError());
              // SEND ERROR MESSAGE TO BOTH CLIENTS
          }
  
          user = !user;
      }
      // Shutdown our socket
      shutdown(serverSocket, SD_SEND);
  
      // Close our socket entirely
      closesocket(serverSocket);
  
      WSACleanup();
      system("pause");
      return 0;
  }
  </code></pre>