title: Client-server Java programm using sockets stucks while runnig, but works while debbuging
body: <p>I have created a simple utility, that implements server and client. Server waits for incomming connection, client connects and if there are any files in folder on server - they are sending to client.
  I used <strong>DataInput/OutputStreams</strong> - <strong>writeUTF/readUTF</strong> for sending messages between client and server, <strong>writeLong/readLong</strong> - to send file size, <strong>write/read</strong> to send data.
  The problem is that when I run both client and server - working stucks after sending one or two files. But if I trace the client and server in IDE step-by-step it works great, and it also works if I transfer files with buffer size in one byte - so it works really slowly, but it does not stuck.
  I use Eclipse IDE, jdk 1.7 on client side and 1.8 on server side (tried 1.7 also), Ubuntu linux and Windows 2003 Server on two instances of VMWare players.
  I also tried to add Thread.sleep(10000) and flush() anything might be flushed, even added <strong>socket.setTcpNoDelay(true);</strong> but no result.
  It looks like server sends the next file name and waits for <strong>echo()</strong> from client, but client does not get it and stucks on <strong>readUTF()</strong>. But, if I do it in manual mode step-by-step it works fine.
  Any ideas? Thank you!</p>
  
  <p>Client source:</p>
  
  <pre><code>/**
   * Method receives a file from server
   * @param outWriter
   * @param inBufferedReader
   * @param inStream
   * 
   */
  private void receiveFileFromServer(DataOutputStream out, DataInputStream in) {
      try {
          String fileName = in.readUTF(); ///&lt;----- stucks here *************
          if (awlConnectionLogger.isInfoEnabled()) {
              awlConnectionLogger.info("Receiving file "+fileName);
          }
          out.writeUTF(fileName);
          out.flush();
  
          Long fileSize = in.readLong();
          if (awlConnectionLogger.isInfoEnabled()) {
              awlConnectionLogger.info("Filesize "+fileSize);
          }
  
          out.writeUTF(fileSize.toString());
          out.flush();
  
          File localFile = new File(fileName);
          FileOutputStream fileOS = new FileOutputStream(localFile);
  
          byte[] buffer = new byte [2048];
          int bytesRead = 0;
  
          try {
                  for (int i = 0; i&lt;fileSize/buffer.length; i++) {
                  bytesRead = in.read(buffer, 0, buffer.length);
                  fileOS.write(buffer,0, bytesRead);
                  }
                  bytesRead = in.read(buffer, 0, (int)(fileSize%buffer.length));
                  fileOS.write(buffer,0, bytesRead);
                  System.out.println("----------------------- "+bytesRead);
          } catch (IOException e) {
              awlConnectionLogger.error("Error reading file "+fileName);
          };
          fileOS.close();
          if (awlConnectionLogger.isInfoEnabled()) {
              awlConnectionLogger.info("File received.");
          }
      } catch (IOException e) {
          awlConnectionLogger.error("Error reading Stream or socket closed");
  
      }
  }
  
  /**
   * Method sends a @param command to server, waits for answer, then if answer is not null and equals @param answer it logs @param messageOk else @param messageError
   * returns true if everything is ok, else returns false
   * @param outWriter
   * @param inBufferedReader
   * @throws IOException
   */
  private boolean sendCommandReceiveAnswer(String command, String answer, String messageOk,String messageError, 
                                          DataOutputStream out, DataInputStream in) throws IOException {
      out.writeUTF(command); //Hello handshake
      out.flush();
      String data = in.readUTF();
      if ((data!=null)&amp;&amp;(data.equals(answer))) {
          if (awlConnectionLogger.isInfoEnabled()) {
              awlConnectionLogger.info(messageOk);
          }
          return true;
      } else {
          awlConnectionLogger.error(messageError);
          return false;
      }
  }
  
  /**
   * Try to establish connection with the awl-server according to detected RDP session
   * @param serverIP - ip address of server
   * @param user - username
   */
  private void establishConnection(String serverIP, String user) {
      if (awlConnectionLogger.isInfoEnabled()) {
          awlConnectionLogger.info("Trying to establish awl-connection to "+serverIP+" as "+user);
      }
      Integer remoteAwlPort = Integer.parseInt(Config.awlPort); 
      try (Socket awlServerSocket = new Socket(serverIP,remoteAwlPort)) {//Creating autocloseable socket
  
          this.awlServerSocket = awlServerSocket;
          DataOutputStream out = new DataOutputStream(awlServerSocket.getOutputStream());
          DataInputStream in = new DataInputStream(awlServerSocket.getInputStream());
  
          if (!sendCommandReceiveAnswer("Hello-awl-client", "Hello-awl-server", "Server hello OK.", 
                                          "Unknown server type. Closing thread.", out, in)) {
              return;
          }; //Hello handshake - return if not successful
          if (!sendCommandReceiveAnswer(user, "User-ok", "Username sent.", "Error sending username. Closing thread.", out, in)) {
              return;
          }   //Sending username
          String hostName = InetAddress.getLocalHost().getHostName(); //Getting client local hostname
          if (!sendCommandReceiveAnswer(hostName, "Hostname-ok", "Local hostname "+hostName+" sent. Ready to receive files.", 
                                          "Error sending hostname. Closing thread.", out, in)) {
              return;
          } //Sending hostname
          awlServerSocket.setTcpNoDelay(true);
          while (isActive) {
  
              receiveFileFromServer(out, in); //receiving files
              out.writeUTF("Ready");
              out.flush();
          }
  
      } catch (UnknownHostException e) {
          awlConnectionLogger.error("Error in server IP adress");
      } catch (SocketException e) {
          awlConnectionLogger.error("Problem accessing or creating Socket.");
      } catch (IOException e) {
          awlConnectionLogger.error("General IO Error or Socket closed");
      };
  }
  </code></pre>
  
  <p>Server source:</p>
  
  <pre><code>void Echo (DataInputStream in) {
          String echo = null;
          try {
              echo = in.readUTF();
              System.out.println("Echo:"+echo);
          } catch (IOException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          }
      }
  
      private void sendToClient (File file, DataOutputStream out, DataInputStream in) {
          System.out.println("Sending file name "+file.getName());
          try {
  
              out.writeUTF(file.getName());
              out.flush();
              Echo (in); //&lt;--------- stucks here *******************
              out.writeLong(file.length());
              out.flush();
              Echo (in);
          } catch (IOException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          }
          System.out.println("Sending file");
          byte [] buffer = new byte [2048];
          FileInputStream fileIS = null;
          try {
              fileIS = new FileInputStream(file);
          } catch (FileNotFoundException e) {
              System.out.println("Error opening file "+file);
              e.printStackTrace();
          }
  
          try {
              Integer bytesRead;
              int i = 0;
              while((bytesRead=fileIS.read(buffer))&gt;0) {
                  out.write(buffer, 0, bytesRead);
              }
              out.flush();
          } catch (IOException e) {
              System.out.println("Error reading file "+file);
              e.printStackTrace();
          };
          System.out.println("File sent.");
          try {
              fileIS.close();
          } catch (IOException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          }
      }
  
  
      public void run () {
  
          DataOutputStream out = null;
          DataInputStream in = null;
          String data = null;
          String user = null;
          String clientHostName = null;
  
          try {
              socket.setTcpNoDelay(true);
              out = new DataOutputStream(socket.getOutputStream());
              in = new DataInputStream(socket.getInputStream());
          } catch (IOException e1) {
              e1.printStackTrace();
          }
          System.out.println("New incoming connection thread starded.");
  
          try {
              data = in.readUTF();
              if (data.equals("Hello-awl-client")) {
                  System.out.println("Client hello OK.");
                  out.writeUTF("Hello-awl-server");
                  out.flush();
                  user = in.readUTF();
                  System.out.println("User: "+user);
                  out.writeUTF("User-ok");
                  out.flush();
                  clientHostName = in.readUTF();
                  System.out.println("Client hostname: "+clientHostName);
                  System.out.println("Server hostname: "+InetAddress.getLocalHost().getHostName());
                  out.writeUTF("Hostname-ok");
                  out.flush();
                  System.out.println("Ready to send files.");
                  while (isActive) {
                      File file = new File(ServerConfig.pdfFolder+"//"+user);
                      if (!file.isDirectory()) {
                          System.out.println("Error in path to user directory. Exiting thread");
                          return;
                      }
                      File [] files = file.listFiles();
                      for (File fileItem:files) {
                          while (!fileItem.renameTo(fileItem)) {
                              Thread.sleep(1000);
                          }; //waits while file is not busy to send it
                          if (socket.isOutputShutdown()) {
                              out = new DataOutputStream(socket.getOutputStream());
                          }
                          sendToClient (fileItem, out, in);
                          if (fileItem.delete()) {
                              System.out.println("File deleted.");
                          } else {
                              System.out.println("File not deleted.");
                          }
                          data = in.readUTF();
                          System.out.println(data);
                      }
                  }
              } else {
                  System.out.println("Unknown connection type. Closing thread.");
                  return;
              }
          } catch (IOException | InterruptedException e) {
              System.out.println("Connection error or can't sleep thread. Closing thread.");
              return;
          }
  
      }
  </code></pre>