title: socket closed exception after transferring one file from server to client
body: <p>I am implementing two programs; Client and Server, and client asks for a file from server to download in local file system.</p>
  
  <p>After downloading one file, the client should be able to download another file if it wishes to..</p>
  
  <p>However after it downloads the file, Server gives me an exception says</p>
  
  <pre><code>java.net.SocketException: Socket closed
  </code></pre>
  
  <p>Here's my code..</p>
  
  <p>Client:</p>
  
  <pre><code>        byte[] aByte = new byte[0];
          int bytesRead;
          String msg;
          FileOutputStream fos = null;
          BufferedOutputStream bos = null;
          while (!(msg = input.readLine()).equals("end")) {
  
              String myFolderName = "ServerFolder";
              File folder=new File(myFolderName);   
              if (!folder.exists()){ 
                  folder.mkdir();
              }
              System.out.println("file downloading");
              try {
  
                  System.out.println("1");
                  fos = new FileOutputStream("ServerFolder/"+fileToDownload);
                  bos = new BufferedOutputStream(fos);
  
                  System.out.println("MSG: "+msg);
  
                  bytesRead = in.read(aByte, 0, aByte.length);
  
                  System.out.println("2");
  
  
  
                  do {
                      baos.write(aByte);
                      bytesRead = in.read(aByte);
                  } while (bytesRead != -1);
  
                  System.out.println("3");
                  bos.write(baos.toByteArray());
                  bos.flush();
              } catch (IOException ex) {
                  System.err.println(ex.getMessage());
              }
  
  
          }
          bos.close();
  
      } catch (IOException ex) {
          System.err.println(ex);
      }    
  </code></pre>
  
  <p>Server:</p>
  
  <pre><code>if (outToClient != null) {
                      System.out.println("2");
                      File myFile = new File(msg);
                      byte[] mybytearray = new byte[(int) myFile.length()];
  
  
                      try {
                          fis = new FileInputStream(myFile);
                      } catch (FileNotFoundException ex) {
                          // Do exception handling
                      }
                      System.out.println("3");
                      bis = new BufferedInputStream(fis);
  
                      try {
                          bis.read(mybytearray, 0, mybytearray.length);   
  
                          System.out.println("mybytearray.length: "+(int) myFile.length());
                          out.write((int) myFile.length()+"\r\n");    
                          out.write("end\r\n");
                          out.flush();
  
  
                          outToClient.write(mybytearray, 0, mybytearray.length);
                          outToClient.flush();
  
                          s.shutdownOutput();
  
                          outToClient.close();
  
  
                          System.out.println("4");
  
                      } catch (IOException ex) {
                          System.out.println("5");
                          System.err.println(ex);
                      }
  </code></pre>
  
  <p>(All connection stuff are done in the beginning of each method)</p>
  
  <p>I had </p>
  
  <pre><code>s.close();
  </code></pre>
  
  <p>in the Server but I deleted it just in case it causes the error but its not..</p>
  
  <p>I am presuming that </p>
  
  <pre><code>outToClient.close();
  </code></pre>
  
  <p>is not causing it either?...</p>
  
  <p>Also I googled this problem and some people suggested to tell the client the size of the file before the server sends the file.. but it didn't work as well.. so I deleted that part as well(or maybe I did it wrong..)</p>
  
  <p>Thanks:)</p>