title: File transfer Server to client - java
body: <p>I am trying to write the server client file transfer program in java.<br>
  Role of Server:-<br>
      a) Serve a file to a client (one).<br>
      b) Server will send file 1 byte at a time.<br>
      c) Should be able to send the file more than once.    </p>
  
  <p>Role of Client:-<br>
      a) Client downloads a file<br>
      b) Client should buffer the file before writing to disk( &lt;= 100kb)  I.e. buffer 100KB and write to disk then repeat this till 1MB  </p>
  
  <p>I wrote the code to transfer the file from Server to Client but there seem to have some issues with the program. Server starts sending the data but its lost in the transaction. Also the content of file to be transferred is erased. As I couldn't see any specific error on console so I couldn't figure out the bug. If anyone guide me whether this approach is correct or if not, please suggest proper changes. </p>
  
  <pre><code>*Server* 
  public class Server {
  
  
  public static void main(String[] args) throws IOException {
      ServerSocket serverSocket = null;
      Socket socket = null;
      InputStream inputStream = null;
      FileOutputStream fileOutputStream = null;
      BufferedOutputStream bufferedOutputStream = null;
      int sizeBuffer = 0;
  
      try{
  
              try {
                      serverSocket = new ServerSocket(55555);
                  } catch (IOException ex) {
                      System.out.println("Error: Unable to Connect to Server ");
                  }
              System.out.println("Created Socket");
              try {
                      socket = serverSocket.accept();
                  } catch (IOException ex) {
                      System.out.println("Error: Unable to connect to client ");
                  }
              System.out.println("Accepted Client Connection ");
              try {
                      inputStream = socket.getInputStream();
                      sizeBuffer = socket.getReceiveBufferSize();
                      System.out.println("Size of Buffer " + sizeBuffer);
                  } catch (IOException ex) {
                          System.out.println("Error: unable to ger socket input stream ");
                  }       
  
              try {
                      fileOutputStream = new FileOutputStream("D:/ServerFile.txt");
                      bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
  
                  } catch (FileNotFoundException ex) {
                          System.out.println("File not found. ");
                  }
  
              byte[] bytes = new byte[sizeBuffer];
  
              int count;
  
              while ((count = inputStream.read(bytes)) &gt; 0) {
                      bufferedOutputStream.write(bytes, 0, count);
              }
  
              System.out.println("Done");
      }// end of first try
  
      finally{
      bufferedOutputStream.flush();
      bufferedOutputStream.close();
      inputStream.close();
      socket.close();
      serverSocket.close();
      }
  }
  
  }
  </code></pre>
  
  <p>And here is client side code !</p>
  
  <pre><code>*Client*
  public class Client {
  public static void main(String[] args) throws IOException {
  
  FileInputStream fileInputStream = null;
  BufferedInputStream bufferedInputStream = null;
  BufferedOutputStream bufferedOuptputStream = null;
  Socket socket = null;
  int count;
  
  try{
      try {
             socket = new Socket("127.0.0.1", 55555);
          } catch (IOException ex) {
              System.out.println("Error: Unable to create Socket ");
          }
  
              File file = new File("D:/ClientFile.txt");
              long fileLength = file.length();
              System.out.println(fileLength);
              if ( Integer.MAX_VALUE &lt; fileLength ) {
                      System.out.println("Error: Exceeded the size of transfer");
              }
  
  
              byte[] bytes = new byte[(int) fileLength];
              try{
                          fileInputStream = new FileInputStream(file);
                  }catch (IOException ex){
                      System.out.println("Error: Unable to open fileInputStream");
                  }
              bufferedInputStream = new BufferedInputStream(fileInputStream);
              bufferedOuptputStream = new BufferedOutputStream(socket.getOutputStream());
  
  while ((count = bufferedInputStream.read(bytes)) &gt; 0) {
      bufferedOuptputStream.write(bytes, 0, count);
  }
  }
  
  finally{
          bufferedOuptputStream.flush();
          bufferedOuptputStream.close();
          fileInputStream.close();
          bufferedInputStream.close();
          socket.close();
  }
  
  }
  }
  </code></pre>