title: C++ Sockets - Server doesn't accept multiple clients (linux)
body: <p>I have working server and client code. The server and client can connect and chat with each other correctly. But when I open another client terminal, the client is says <code>Awaiting confirmation from the server</code> and nothing else. Although server and client #1 can still chat.</p>
  
  <p>I searched on multi-threading but the examples or code snippets they show is advanced. Maybe a little explanation or an example will help a lot!</p>
  
  <p>The code below is working. I have a working server but it only accepts one connection. How do I make the server to allow multiple connection? So that I can make the program look like a group chat.</p>
  
  <p>client.cpp (when client #2 connects, the code freezes at line 40)</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;string.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;sys/socket.h&gt;
  #include &lt;netinet/in.h&gt;
  #include &lt;arpa/inet.h&gt;
  #include &lt;netdb.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;unistd.h&gt;
  
  using namespace std;
  
  int main()
  {
      char a;
      int client;
      int portNum = 1500;
      int bufsize = 1024;
      char* buffer = new char[bufsize];
      bool isExit = false;
      char* ip = "127.0.0.1";
  
      struct sockaddr_in direc;
  
      if ((client = socket(AF_INET, SOCK_STREAM, 0)) &lt; 0) {
          cout &lt;&lt; "\nError creating socket..." &lt;&lt; endl;
          exit(0);
      }
  
      cout &lt;&lt; "\nSocket created successfully..." &lt;&lt; endl;
      direc.sin_family = AF_INET;
      direc.sin_port = htons(portNum);
      inet_pton(AF_INET, ip, &amp;direc.sin_addr);
  
      if (connect(client,(struct sockaddr *)&amp;direc, sizeof(direc)) == 0)
          cout &lt;&lt; "Connection to the server " &lt;&lt; inet_ntoa(direc.sin_addr) &lt;&lt; endl;
  
      cout &lt;&lt; "Awaiting confirmation from the server..." &lt;&lt; endl; //line 40
      recv(client, buffer, bufsize, 0);
  
      cout &lt;&lt; "\n=&gt; Enter # to terminate the connection\n" &lt;&lt; endl;
  
      do {
          cout &lt;&lt; "Client: ";
          do {
              cin &gt;&gt; buffer;
              send(client, buffer, bufsize, 0);
              if (*buffer == '#') {
                  send(client, buffer, bufsize, 0);
                  *buffer = '*';
                  isExit = true;
              }
          } while (*buffer != 42);
  
          cout &lt;&lt; "Server: ";
          do {
              recv(client, buffer, bufsize, 0);
              cout &lt;&lt; buffer &lt;&lt; " ";
              if (*buffer == '#') {
                  *buffer = '*';
                  isExit = true;
              }
  
          } while (*buffer != 42);
          cout &lt;&lt; endl;
  
      } while (!isExit);
      cout &lt;&lt; "=&gt; Connection terminated.\nGoodbye";
  
      close(client);
      return 0;
  }
  </code></pre>
  
  <p>server.cpp</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;string.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;sys/socket.h&gt;
  #include &lt;netinet/in.h&gt;
  #include &lt;arpa/inet.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;unistd.h&gt;
  
  using namespace std;
  
  int main()
  {
      int client, server;
      int bufsize = 1024;
      int portNum = 1500;
      bool isExit = false;
      char* buffer = new char[bufsize];
  
      struct sockaddr_in direc;
      socklen_t tamano;
      pid_t pid;
  
      if ((client = socket(AF_INET, SOCK_STREAM, 0)) &lt; 0) {
          cout &lt;&lt; "\nError establishing socket..." &lt;&lt; endl;
          exit(1);
      }
  
      cout &lt;&lt; "\nSocket server has been created..." &lt;&lt; endl;
  
      direc.sin_family = AF_INET;
      direc.sin_addr.s_addr = htons(INADDR_ANY);
      direc.sin_port = htons(portNum);
  
      if ((bind(client, (struct sockaddr*)&amp;direc,sizeof(direc))) &lt; 0) {
          cout &lt;&lt; "\nError binding connection..." &lt;&lt; endl;
          return -1;
      }
  
      tamano = sizeof(direc);
      cout &lt;&lt; "Looking for clients..." &lt;&lt; endl;
      listen(client, 1);
  
      while ((server = accept(client,(struct sockaddr *)&amp;direc,&amp;tamano)) &gt; 0) {
          strcpy(buffer, "Server connected...\n");
          send(server, buffer, bufsize, 0);
          cout &lt;&lt; "Connected with the client, you are good to go..." &lt;&lt; endl;
          cout &lt;&lt; "Enter # to end the connection\n" &lt;&lt; endl;
  
          cout &lt;&lt; "Client: ";
          do {
              recv(server, buffer, bufsize, 0);
              cout &lt;&lt; buffer &lt;&lt; " ";
              if (*buffer == '#') {
                  *buffer = '*';
                  isExit = true;
              }
          } while (*buffer != '*');
  
          do {
              cout &lt;&lt; "\nServer: ";
              do {
                  cin &gt;&gt; buffer;
                  send(server, buffer, bufsize, 0);
                  if (*buffer == '#') {
                      send(server, buffer, bufsize, 0);
                      *buffer = '*';
                      isExit = true;
                  }
              } while (*buffer != '*');
  
              cout &lt;&lt; "Client: ";
              do {
                  recv(server, buffer, bufsize, 0);
                  cout &lt;&lt; buffer &lt;&lt; " ";
                  if (*buffer == '#') {
                      *buffer == '*';
                      isExit = true;
                  }
              } while (*buffer != '*');
          } while (!isExit);
  
          cout &lt;&lt; "\n=&gt; Connection terminated... " &lt;&lt; inet_ntoa(direc.sin_addr);
          close(server);
          cout &lt;&lt; "\nGoodbye..." &lt;&lt; endl;
          isExit = false;
      }
  
      close(client);
      return 0;
  }
  </code></pre>
  
  <p>How do I make the server accept multiple connection?</p>
  
  <p>Thanks!</p>