title: read packet from tcp socket in java with acknoledgement
body: <p>We are creating a java listener to read multiple device data those are configured on particular server ip and port.Device following below rule.</p>
  
  <ol>
  <li>device send a login packet.</li>
  <li>server will return ack packet in response.</li>
  <li>after receive ack device will send information packet.</li>
  <li>server reads that data. </li>
  </ol>
  
  <p>on last step we stuck, we are sending the ack but cant get the information packet back from device(though we check the generated ack through opensource sofware).For ref we are attaching code.(if we remove while(true) than get login packet but after that socket connection will close and again device will send login packet but if we keep it then we dont get any packet)</p>
  
  <p>//--------------Main class------------------------------------------</p>
  
  <p>public class Main {</p>
  
  <pre><code>public static void main(String[] args) {
      Server server = new Server(listen_port, pool_size, pm);
      new Thread(server).start();
      logger.info("Server Started .....");
  }
  </code></pre>
  
  <p>}
  //--------------------------------------------------------------</p>
  
  <p>public class Server implements Runnable {</p>
  
  <pre><code>private ServerSocket serverSocket = null;
  public void run()
  </code></pre>
  
  <p>{
      this.m_stop = false;</p>
  
  <pre><code>while (!this.m_stop)
    try {
      this.m_pool.execute(new Handler(this.serverSocket.accept()));
    } catch (IOException e) {
      LOGGER.debug("Unable to accept connection ", e);
    }
  </code></pre>
  
  <p>}
  }</p>
  
  <p>//--------------------------------------------------------------</p>
  
  <p>public class Handler implements Runnable {</p>
  
  <pre><code>private Socket m_clientSocket;
  private String imei;
  
  public Handler(Socket socket) {
      this.m_clientSocket = socket;
  }
  
  public void run() {
      DataOutputStream clientDataOS = null;
      DataInputStream clientDataIS = null;
      try {
          logger.info("data is coming");
          m_clientSocket.setSoTimeout(300000);
          clientDataIS = new DataInputStream(this.m_clientSocket.getInputStream());
          clientDataOS = new DataOutputStream(this.m_clientSocket.getOutputStream());
          while (true) {
              String pkt = "";
              logger.info("Waiting for input strem");
              byte[] byte_pkt = IOUtils.toByteArray(clientDataIS);
              logger.info("Got input stream");
              for (byte b : byte_pkt) {
                  pkt += String.format("%02X ", b);
              }
              logger.info(pkt);
              if (byte_pkt.length &gt; 0) {
                  logger.info("");
                  if (Byte.valueOf(byte_pkt[3]) == 1) {
                      imei = "xyz";
                      logger.info("login packet");
                      byte[] rep_pkt = Utils.getReceptionPacket(byte_pkt);//send back to device
                      clientDataOS.write(rep_pkt);
                      clientDataOS.flush();
                  } else if (Byte.valueOf(byte_pkt[3]) == 34) {
                      logger.info("information packet");
                      Utils.processPackets(byte_pkt);
                  } else {
                      logger.info("Unkown packet format");
                  }
                  logger.info(imei);
              } else {
                  logger.info("InputStream is empty.");
              }
          }
      } catch (SocketException se) {
          logger.error("Failure on reading data", se);
      } catch (IOException e) {
          logger.error("Failure on reading data", e);
      } catch (Exception e) {
          logger.error("Error while processing data", e);
      } finally {
          try {
              IOUtils.closeQuietly(clientDataOS);
              IOUtils.closeQuietly(clientDataIS);
              this.m_clientSocket.close();
          } catch (IOException e) {
              logger.debug("Error when sending out response ::", e);
          }
      }
  }
  </code></pre>
  
  <p>}</p>