title: TCP Socket receiving and processing multiple messages
body: <p>I'm having some troubles in getting all the data coming from a TCP socket.</p>
  
  <p>In my server, I'm reading data from a socket like that:</p>
  
  <pre><code>        int len;
          byte[] buffer = new byte[2000];
          try {
              this.in = new DataInputStream(this.socket.getInputStream());
              this.out = new DataOutputStream(this.socket.getOutputStream());
              running = true;
  
              while (running) {
                  len = in.read(buffer);
                  if (len &lt; 0) {
                      running = false;
                  } else {
                      parsePacket(buffer, len);
                  }
              }
  
          } catch (IOException ex) {
              System.out.println("Catch IOException: " + ex);
              ex.printStackTrace();
          } finally {
              try {
                  System.out.println("Closing");
                  in.close();
                  out.close();
                  socket.close();
              } catch (IOException ex) {
                  System.out.println("Finally IOException: " + ex);
              }
          }
  </code></pre>
  
  <p>The packet format is like that:</p>
  
  <p><strong>[HEADER][DATA][TERMINATOR]</strong></p>
  
  <ul>
  <li>Header --> sequence of characters that identifies the start of the message (no info about length of the packet);</li>
  <li>Data --> Is divided in segments, like: <strong>[Size Seg. 1][Data Seg. 1][Size Seg. 2][Data Seg. 2][Size Seg. 3][Data Seg. 3]....[Size Seg. N][Data Seg. N]</strong></li>
  <li>Terminator --> [0x00] </li>
  </ul>
  
  <p>The data are received very fast (sometimes 200ms or less), so sometimes the <code>read(buffer)</code> fills the <code>buffer</code> with messages like:</p>
  
  <ul>
  <li>[HEADER1][DATA1][TERM1] or,</li>
  <li>[HEADER1][DATA1][TERM1][HEADER2][DATA2][TERM2].............[HEADER N][DATA N][TERM N] or,</li>
  <li>[HEADER1][DATA1][TERM1][HEADER2][DATA2][TERM2].............[HEADER N][DAT (last message incomplete)</li>
  </ul>
  
  <p>The <code>parsePacket()</code> method is able to parse a message with the above format and if it has more messages next, they will be parsed also (recursively). But it will not parse the last message if it is incomplete (I don't want that, but I didn't find a suitable solution until now).</p>
  
  <p>The data coming inside the messages are stored in a MySQL database (using JDBC driver). Each parse of a message can involve multiple queries to the database. As I'm using just one thread to receive, parse and store the data, the execution of the code is not as fast as it should... The data should be received and stored as quickly as possible.</p>
  
  <p>Some points that I would like to discuss:</p>
  
  <ul>
  <li>What could be the best approach to get all the messages without lose some of them?</li>
  <li>How could I improve the way that the data is received and stored? (The data should be stored as quickly as possible!)</li>
  </ul>