title: C++ TCP Winsock Server receiving the same packet many times
body: <p>I have written a very simple C++ server which I am connecting to from a Java application.  The C++ server uses winsock2.  I am sending UTF8 encoded numbers to the server from my client and on receipt of these numbers I would like the server to perform an action.  However my server seems to be receiving a series of numbers as one.  At the moment I have the server listening every 1 millisecond for a new message.</p>
  
  <p>This is my C++ server code which receives the message:     </p>
  
  <pre><code>  bool receive()
       {
            char buffer[1024];
            int inDataLength=recv(Socket,buffer,sizeof(buffer),0);
            if(buffer[0] != '\0')
            {
                       std::cout&lt;&lt;"Client: ";
                       std::cout &lt;&lt; buffer;
                       sendKey(string(buffer));  
            }
            else if (inDataLength == 0) //Properly closed connection
            {
               std::cout&lt;&lt;"Connection lost..\r\n";
               return false;
            }    
            return true;
       }
  </code></pre>
  
  <p>This is called within a loop like so:</p>
  
  <pre><code> while ( receive() )
  {
        Sleep(1);
  }
  </code></pre>
  
  <p>This is my java client code to send a message where out is <code>OutputStream = socket.getOutputStream():</code></p>
  
  <pre><code>public void send(String msg)
  {
          try {
              out.write( msg.getBytes("UTF8") );
              out.flush();
              Thread.sleep(100);
          } catch (SocketException e) {
              Global.error("Connection error..");
              //e.printStackTrace();
          } catch (UnsupportedEncodingException e) {
  
          } catch (IOException e) {
              Global.error("Never connected..\r\n");
          } catch (Exception e)
          { Global.error("Sending failed..\r\n"); }
      }
  </code></pre>
  
  <p>What I am getting is the server receiving for example the number 1, then 2, then 12, then 121 etc.. in no specific pattern except once the server is receiving 2 numbers at once it will never start receiving only one again.  This is the only place in my java code where anything is sent to the server and I flush the buffer after each message so I think the issue is on my server but I'm at a loss as to the problem.</p>
  
  <p>Any help would be much appreciated.</p>
  
  <p>Thanks.</p>