title: C++ Multi-Client TCP Server
body: <p>I want to make a server and client program with TCP protocol using C++. The server must be able to handle multiple client at once. But the problem is for example, after starting the server, I run 2 clients with the server 's IP address and port as parameters. Next, both clients are sending data to server. At first, both clients could send data to server and the server was able read the data. But, once the server has received data from the second client, it seems that it stopped receiving from the first client. Do you have any solution?</p>
  
  <p>Here is the server code</p>
  
  <pre><code>using namespace std;
  
  void *task1(void *);
  
  static int connFd;
  void error(const char *msg)
  {
      perror(msg);
      exit(1);
  }
  
  int main(int argc, char* argv[])
  {
      int pId, portNo, listenFd;
      socklen_t len; //store size of the address
      bool loop = false;
  struct sockaddr_in svrAdd, clntAdd;
  
  pthread_t threadA[3];
  
  if (argc &lt; 2)
  {
      cerr &lt;&lt; "Syntam : ./server &lt;port&gt;" &lt;&lt; endl;
      return 0;
  }
  
  portNo = atoi(argv[1]);
  
  if((portNo &gt; 65535) || (portNo &lt; 2000))
  {
      cerr &lt;&lt; "Please enter a port number between 2000 - 65535" &lt;&lt; endl;
      return 0;
  }
  
  //create socket
  listenFd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
  
  if(listenFd &lt; 0)
  {
      cerr &lt;&lt; "Cannot open socket" &lt;&lt; endl;
      return 0;
  }
  
  bzero((char*) &amp;svrAdd, sizeof(svrAdd));
  
  svrAdd.sin_family = AF_INET;
  svrAdd.sin_addr.s_addr = INADDR_ANY;
  svrAdd.sin_port = htons(portNo);
  
  //bind socket
  if(bind(listenFd, (struct sockaddr *)&amp;svrAdd, sizeof(svrAdd)) &lt; 0)
  {
      cerr &lt;&lt; "Cannot bind" &lt;&lt; endl;
      return 0;
  }
  
      listen(listenFd, 5);
  
      int noThread = 0;
  
      while (noThread &lt; 3)
      {
          socklen_t len = sizeof(clntAdd);
          cout &lt;&lt; "Listening" &lt;&lt; endl;
  
      //this is where client connects. svr will hang in this mode until           client conn
          connFd = accept(listenFd, (struct sockaddr *)&amp;clntAdd, &amp;len);
  
          if (connFd &lt; 0)
          {
              cerr &lt;&lt; "Cannot accept connection" &lt;&lt; endl;
              return 0;
          }
          else   
          {
              cout &lt;&lt; "Connection successful" &lt;&lt; endl;
          }
  
          pthread_create(&amp;threadA[noThread], NULL, task1, NULL); 
  
          noThread++;
      }
  
      for(int i = 0; i &lt; 3; i++)
      {
          pthread_join(threadA[i], NULL);
      }  
  }
  
  void *task1 (void *dummyPt)
  {
      cout &lt;&lt; "Thread No: " &lt;&lt; pthread_self() &lt;&lt; endl;
      char test[256];
      bzero(test, 256);
      bool loop = false;
      while(!loop)
      {       
          bzero(test, 256);     
          int n = read(connFd, test, 255);
          if (n &lt; 0) error("ERROR reading from socket");
          printf("Here is the message: %s\n",test);            
      }
      cout &lt;&lt; "\nClosing thread and conn" &lt;&lt; endl;
      close(connFd);
  }
  </code></pre>
  
  <p>And the client code</p>
  
  <pre><code>using namespace std;
  
  int main (int argc, char* argv[])
  {
      int listenFd, portNo;
      bool loop = false;
      struct sockaddr_in svrAdd;
      struct hostent *server;
  
      if(argc &lt; 3)
      {
          cerr&lt;&lt;"Syntax : ./client &lt;host name&gt; &lt;port&gt;"&lt;&lt;endl;
          return 0;
      }
  
      portNo = atoi(argv[2]);
  
      if((portNo &gt; 65535) || (portNo &lt; 2000))
      {
          cerr&lt;&lt;"Please enter port number between 2000 - 65535"&lt;&lt;endl;
          return 0;
      }       
  
      //create client skt
      listenFd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
  
      if(listenFd &lt; 0)
      {   
          cerr &lt;&lt; "Cannot open socket" &lt;&lt; endl;
          return 0;
      }
  
      server = gethostbyname(argv[1]);
  
      if(server == NULL)
      {
          cerr &lt;&lt; "Host does not exist" &lt;&lt; endl;
          return 0;
      }
  
      bzero((char *) &amp;svrAdd, sizeof(svrAdd));
      svrAdd.sin_family = AF_INET;
  
      bcopy((char *) server -&gt; h_addr, (char *) &amp;svrAdd.sin_addr.s_addr, server -&gt; h_length);
  
      svrAdd.sin_port = htons(portNo);
  
      int checker = connect(listenFd,(struct sockaddr *) &amp;svrAdd, sizeof(svrAdd));
  
      if (checker &lt; 0)
      {
          cerr &lt;&lt; "Cannot connect!" &lt;&lt; endl;
          return 0;
      }
  
      //send stuff to server
      for(;;)
      {
          char s[300];
          //cin.clear();
          //cin.ignore(256, '\n');
          cout &lt;&lt; "Enter stuff: ";
          bzero(s, 300);
          cin.getline(s, 300);
  
          write(listenFd, s, strlen(s));
      }
  }
  </code></pre>