title: Java Socket File Transfer Doesnt Work Properly
body: <p>I am new with networking in java and i want to achieve a file transfer system that server downloads a file from client (client computer file to server) i searched the internet for a bunch of tutorials but all of them seems to give me a error or dont do anything but i found 10 year old tutorial that worked here is the link:
  <a href="http://www.coderanch.com/t/277351/java-io/java/File-transfer-sockets-streams" rel="nofollow">Click me</a> and i did small change to it that made it more stable but there is a problem that i cannot solve when the file downloads on the server the file is still in use by the server and i noticed that the loop does not quit and doesnt close the file stream when its done now here is the code:</p>
  
  <p>Client:</p>
  
  <pre><code>File fl = new File(splitedstreamcommand[2]);
  FileInputStream fin = new FileInputStream(fl);
  
  int i = -1;
  while((i = fin.read()) != -1){
      output.write(i);
      output.flush();
      //System.out.print((char) i);
  }
  fin.close();
  </code></pre>
  
  <p>The output is a ObjectOutputStream</p>
  
  <p>Server:</p>
  
  <pre><code>File fl = new File("C:\\testfolder\\thetestfiledownloaded.txt");
  //FileOutputStream fos = new FileOutputStream("C:\\testfolder\\thetestfiledownloaded.txt");
  
  int i = -1;
  if(fl.createNewFile()){
      FileOutputStream fout = new FileOutputStream(fl);
  
      while((i=input.read()) != -1){
          //System.out.print((char) i);
          fout.write(i);
          System.out.println("Downloading....");
      }
      //System.out.println("Still downloading....");
      fout.close();
      //fin.close();
  
      System.out.println("File downloaded.");
  }
  </code></pre>
  
  <p>The input is a ObjectInputStream and now the loop that is the problem is here in the server the while loop that is reading bytes it stops when its done reading but it doesnt quit the loop and continue executing the code like the File Downloaded string that should print out and the file is still in use. I think this code is broken because its old but this is the only code that is working to me for some reason soo how can i fix this issue? Thanks. Sorry about the bad question format and explanation if you cannot understand tell me and i can try to format it.</p>