title: Java execution blocked in socket
body: <p>I am creating a simple client server application that let the server receive a file from the client. I implemented almost everything, but there is a moment in the excution, where the code stops, and doesn't proceed anymore.</p>
  <p>I am not much experienced in Java, so maybe it is a trivial matter, but I can't figure out.</p>
  <p>This is the code in the server:</p>
  <pre><code>    public static void receive(String token, String fileName, String filePath) throws IOException{
  
          //Stream for binary data and file transfer
          OutputStream out = new FileOutputStream(basePath + filePath  + fileName);
          InputStream in = socket.getInputStream();
  
          // Variables 
          int bytesRead;
  
          // Bytes for store info to be sent
          byte[] buffer = new byte[1024 * 16];
  
          //-------------------------
          // Send file 
          //------------------------- 
  
          System.out.println(&quot;1&quot;);
  
          while((bytesRead = in.read(buffer)) &gt; 0){
              System.out.println(&quot;2&quot;);
              out.write(buffer, 0, bytesRead);
              System.out.println(&quot;3&quot;);
          }
          System.out.println(&quot;4&quot;);
  
          //-------------------------
          // End of file transfer
          //-------------------------
          
  
          out.close();
      } // send
  </code></pre>
  <p>And this is the client:</p>
  <pre><code>    public static void send(Socket socket, String token, String fileName, String filePath) throws IOException{
  
          // Getting the file to send from the file system
          File file = new File(&quot;C:\\&quot; + filePath + &quot;\\&quot; + fileName);
  
          // Getting length of the file and creating the buffer
          int length = (int) file.length();
          byte[] buffer = new byte[length];
  
          // Setting up channels to transfer data
          InputStream in = new FileInputStream(file);
          OutputStream out = socket.getOutputStream();
  
          // Variables 
          int bytesRead;
  
          //-------------------------
          // Send file 
          //------------------------- 
  
          while ((bytesRead = in.read(buffer)) &gt; 0) {
              out.write(buffer, 0, bytesRead);
          }
  
          //-------------------------
          // End of file transfer
          //-------------------------
          in.close();
      } // send
  </code></pre>
  <p>In the server part, when it comes to the while loop, it prints the number 1, 2, 3, but 4 is never printed. The code get stopped or something similar, and can't proceed anymore. So, other eventual call provided by the main are blocked.</p>
  <p>Repeat, maybe it is a trivial teorical matter, but I can't really understand why. If the loop was infinite, 2 and 3 must have been printed a lot of time, but it happens only one time. Then, I can't really see what is blocking Java from printing 4 and letting the code continuing the run.</p>
  <p>If someone has some idea, I would be increadibly pleased. Thank you in advice</p>