title: File transfer - Java socket programming
body: <p>I have the following Server and Client codes. Client tries to transfer a File say "testprgm.txt" of Size say 2000B to Server, where it saves it as "Test.txt". The problem is I can see the transfer for bytes on both the Server and Client but when I see the size of the Test.txt file after running these codes, it is ZERO.</p>
  
  <h2>Server Program:</h2>
  
  <pre><code>import java.io.*;
  import java.net.*;
  
  public class ServerTest {
  
      public static void main(String[] args) {
          System.out.println("**********Server Program**************");
          int byteRead = 0;
          try {
              ServerSocket serverSocket = new ServerSocket(9999);
              if (!serverSocket.isBound())
                  System.out.println("Sever Socket not Bounded...");
              else
                  System.out.println("Server Socket bounded to Port : " + serverSocket.getLocalPort());
  
              Socket clientSocket = serverSocket.accept();
              if (!clientSocket.isConnected())
                  System.out.println("Client Socket not Connected...");
              else
                  System.out.println("Client Socket Connected : " + clientSocket.getInetAddress());
  
              while (true) {
                  InputStream in = clientSocket.getInputStream();
  
                  OutputStream os = new FileOutputStream("&lt;DESTINATION PATH&gt;/Test.txt");
                  byte[] byteArray = new byte[100];
  
                  while ((byteRead = in .read(byteArray, 0, byteArray.length)) != -1) {
                      os.write(byteArray, 0, byteRead);
                      System.out.println("No. of Bytes Received : " + byteRead);
                  }
                  synchronized(os) {
                      os.wait(100);
                  }
                  os.close();
                  serverSocket.close();
                  //System.out.println("File Received...");
              }
  
          } catch (Exception e) {
              System.out.println("Server Exception : " + e.getMessage());
              e.printStackTrace();
          }
      }
  
  }
  </code></pre>
  
  <h2>Client Program :</h2>
  
  <pre><code>import java.io.*;
  import java.net.*;
  
  public class Clientprgm {
  
  public static void main(String[] args)
  {
      Socket socket;
      try
      {
          socket = new Socket("SERVER IP ADDRESS&gt;", 9999);            
          if(!socket.isConnected())
              System.out.println("Socket Connection Not established");
          else
              System.out.println("Socket Connection established : "+socket.getInetAddress());
  
          File myfile = new File("&lt;SOURCE PATH&gt;/testprgm.txt");       //local file path.
  
  
          if(!myfile.exists())
              System.out.println("File Not Existing.");
          else
              System.out.println("File Existing.");
  
          byte[] byteArray = new byte[1024];
  
          FileInputStream fis = new FileInputStream(myfile);
          BufferedInputStream bis = new BufferedInputStream(fis);
          OutputStream os = socket.getOutputStream();
          int trxBytes =0;
          while((trxBytes = bis.read(byteArray, 0, byteArray.length)) !=-1)
          {           
          os.write(byteArray, 0, byteArray.length);
          System.out.println("Transfering bytes : "+trxBytes );
          }
          os.flush();
          bis.close();
          socket.close();
  
          System.out.println("File Transfered...");
      }
      catch(Exception e)
      {
          System.out.println("Client Exception : "+e.getMessage());
      }       
  }
  }
  </code></pre>