title: Make socket server accept multiple clients
body: <p>I'd like to change the <a href="http://www.codeproject.com/KB/IP/client_server_socket.aspx" rel="nofollow noreferrer">socket class</a> I am using to accept an infinite amount of clients. At the moment it allows one client, and once that client disconnect the server exits.</p>
  
  <pre><code>#include "stdafx.h"
  
  #include "mySocket.h"
  #include "myException.h"
  #include "myHostInfo.h"
  
  void main()
  {
  
  #ifdef WINDOWS_XP
      // Initialize the winsock library
      WSADATA wsaData;
      try
      {
          if (WSAStartup(0x101, &amp;wsaData))
          {
              myException* initializationException = new myException(0,"Error: calling WSAStartup()");
              throw initializationException;
          }
      }
      catch(myException* excp)
      {
          excp-&gt;response();
          delete excp;
          exit(1);
      }
  #endif
  
      // get local server information
      myHostInfo uHostAddress;
      string localHostName = uHostAddress.getHostName();
      string localHostAddr = uHostAddress.getHostIPAddress();
      cout &lt;&lt; "------------------------------------------------------" &lt;&lt; endl;
      cout &lt;&lt; "   My local host information:" &lt;&lt; endl;
      cout &lt;&lt; "       Name:    " &lt;&lt; localHostName &lt;&lt; endl;
      cout &lt;&lt; "       Address: " &lt;&lt; localHostAddr &lt;&lt; endl;
      cout &lt;&lt; "------------------------------------------------------" &lt;&lt; endl;
  
      // open socket on the local host
      myTcpSocket myServer(PORTNUM);
      cout &lt;&lt; myServer;
  
      myServer.bindSocket();
      cout   &lt;&lt; endl &lt;&lt; "server finishes binding process... " &lt;&lt; endl;
  
      myServer.listenToClient();
      cout   &lt;&lt; "server is listening to the port ... " &lt;&lt; endl;
  
      // wait to accept a client connection.
      // processing is suspended until the client connects
      cout   &lt;&lt; "server is waiting for client connecction ... " &lt;&lt; endl;
  
      myTcpSocket* client;    // connection dedicated for client communication
      string clientHost;      // client name etc.
      client = myServer.acceptClient(clientHost);
  
      cout   &lt;&lt; endl &lt;&lt; "==&gt; A client from [" &lt;&lt; clientHost &lt;&lt; "] is connected!" &lt;&lt; endl &lt;&lt; endl;
  
      while(1)
      {
          //Send message to the client
          client-&gt;sendMessage(std::string("Test"));
  
          // receive from the client
          string clientMessageIn = "";
          int numBytes = client-&gt;recieveMessage(clientMessageIn); //Get message from client, non-blocking using select()
          if ( numBytes == -99 ) break;
  
          if(clientMessageIn != "")
          {
              std::cout &lt;&lt; "received: " &lt;&lt; clientMessageIn &lt;&lt; std::endl; //What did we receive?
  
              /* Do somethign with message received here */
          }
      }
  
  #ifdef WINDOWS_XP
      // Close the winsock library
  
      try
      {
          if (WSACleanup())
          {
              myException* cleanupException = new myException(0,"Error: calling WSACleanup()");
              throw cleanupException;
          }
      }
      catch(myException* excp)
      {
          excp-&gt;response();
          delete excp;
          exit(1);
      }
  
  #endif
  }
  </code></pre>
  
  <p>How do I change the main() function so that it is constantly waiting for new clients to connect, and once they do, create a new thread for him (the client), or a new handler socket (whatever that may be).</p>
  
  <p>I did find <a href="https://stackoverflow.com/questions/5815872/asynchronous-server-socket-multiple-clients">this</a> thread to be informative, but I lack the required knowledge of sockets to actually implement it in the above code.</p>
  
  <p>The answer states <code>When doing socket communication, you basically have a single listener socket for all incoming connections, and multiple handler sockets for each connected client.</code></p>
  
  <p>So I am guessing in my code;</p>
  
  <pre><code>myTcpSocket myServer(PORTNUM);
  myServer.bindSocket();
  myServer.listenToClient();
  </code></pre>
  
  <p>Would be the <code>listener socket</code></p>
  
  <p>But where/how would I fork the client who is connecting off to a <code>handler socket</code> ?</p>
  
  <p>I am sorry for not being able to show more effort on my part, I don't like coming across as lazy. But for all the hours I have searched and the trial and error resulting from that, I don't have much to show for it.</p>