title: C++ multi client chat using TCP socket
body: <p>Im trying to do multi client chat in c++ using TCP socket.
  i have download the source of the socket implementation from <a href="https://cs.baylor.edu/~donahoo/practical/CSockets/practical/" rel="nofollow noreferrer">this site</a>.</p>
  
  <p>The problem is when i try to send message to the server from the client,
  the "ecko" that i recive from the server is endless string of spaces.</p>
  
  <p>i tried to debug the client code, the client read the input properly.
  In the first few message the server send back to the client his message,
  but after few messages the client get back endless spaces.
  i tried to use memset to nullify(put zero in all the array), but its making it worse, the server dont recive messages at all.</p>
  
  <p>Would appreciate help (:</p>
  
  <p>This is the server side:</p>
  
  <pre><code> #include "PracticalSocket.h"
  #include &lt;stdio.h&gt;
  #include &lt;process.h&gt;
  #include &lt;Windows.h&gt;
  using namespace std;
  #pragma comment(lib,"ws2_32.lib")
  
  
  TCPSocket* MyClients[20];
  int ClientCount = 0;
  
   void connectCLient(void* pValue){  
      int nI,Flag;
      char st[1024];
      //memset(st,0,1024); // doing problems
      TCPSocket* pServerClient = (TCPSocket*)pValue;  
      MyClients[ClientCount] = pServerClient;
      ClientCount++;
      try{
          while (true)
          {
               Flag = pServerClient-&gt;recv(st,strlen(st));
               if(Flag&gt;1){
                  printf("%s\n",st);
                  for(nI = 0; nI&lt; ClientCount ; nI++){
                      MyClients[nI]-&gt;send(st,strlen(st)+1);
               }
          }
  
  
          }
      }
      catch(...){
          puts("one client lefttt");
      }
   }
  
  int main(int argc, char* argv[])
  {
      TCPServerSocket* pServer = new TCPServerSocket(8546);
      int nClientCounter = 0;
      printf("Start TCP Server ... on Port %d\n", 8546);
  
      try{
          while(true)
          {
  
              printf("Wait for new TCP Clients ... \n");
              TCPSocket* pClient = pServer-&gt;accept();     
              _beginthread(connectCLient,0,(void*)pClient);
              printf("Client %d Connected ... \n", ++nClientCounter);
          }
      }
      catch(...){
          puts("one client left");
      }
  
      return 0;
  }
  </code></pre>
  
  <p>This is the Client side:</p>
  
  <pre><code>#include "PracticalSocket.h"
  #include &lt;stdio.h&gt;
  #include &lt;process.h&gt;
  #include &lt;Windows.h&gt;
  using namespace std;
  #pragma comment (lib, "ws2_32.lib")
  
  
  void ReciveMessages(void * pValue ){
      char recvM[1024];
      TCPSocket* pClient = (TCPSocket*)pValue;
      while(true){
          pClient-&gt;recv(recvM,strlen(recvM));
          printf("%s\n",recvM);
      }
  }
  
  
  int main(int argc, char* argv[])
  {
      try
      {
          TCPSocket * cClient = new TCPSocket();
          cClient-&gt;connect("127.0.0.1",8546);
          _beginthread(ReciveMessages,0,(void*)cClient);
          char st[1024];
          memset(st,0,1024);
          while(true)
          {
  
              printf("Press Text --&gt;");
              fgets(st, sizeof st, stdin);
              cClient-&gt;send(st,strlen(st)+2); 
          }
      }
      catch(...)
      {
          printf("Socket Error..!");
          system("pause");//run cmd comment - stop the system
      }
      return 0;
  }
  </code></pre>