title: java socket programming to send and receive file between two machines
body: <p>I am trying to communicate between two machines using socket programming.</p>
  
  <p>What I basically need is both machines should be able to send and receive files. The code I am pasting below is not showing any error but the server side program seems to be running indefinitely, i.e., it is not terminating. It got stuck on the line marked with comment stuck here.</p>
  
  <p>In this code, initially, server is sending the file named "file.txt" and client is receiving it and saving the file with name "copy.txt". Later client is sending a file named "file2.txt" and server is receiving and saving it with name "copy2.txt".</p>
  
  <p>Can someone please tell me the error and suggest some improvements?</p>
  
  <pre><code>//server side code
  
  import java.net.*; 
  import java.io.*; 
  public class server 
  { 
  public static void main (String [] args ) throws IOException 
  { 
  
      //sending file started
      ServerSocket serverSocket = new ServerSocket(16167);
      Socket socket = serverSocket.accept(); 
      System.out.println("Accepted connection : " + socket); 
      File transferFile = new File ("/Users/abhishek/desktop/file.txt"); 
      byte [] bytearray = new byte [(int)transferFile.length()]; 
      FileInputStream fin = new FileInputStream(transferFile); 
      BufferedInputStream bin = new BufferedInputStream(fin); 
      bin.read(bytearray,0,bytearray.length); 
      OutputStream os = socket.getOutputStream(); 
      System.out.println("Sending Files..."); 
      os.write(bytearray,0,bytearray.length); 
      os.flush(); 
      System.out.println("File transfer complete"); 
      //socket.close();
      //sending comleted
  
      //receiving file started
      int filesize=1022386; 
      int bytesRead=0; 
      int currentTot = 0;  
      byte [] bytearray1 = new byte [filesize]; 
  
      InputStream is = socket.getInputStream(); 
  
      FileOutputStream fos = new FileOutputStream("/Users/abhishek/desktop/copy2.txt"); 
      //fos.flush(); 
      BufferedOutputStream bos = new BufferedOutputStream(fos); 
      //bos.flush(); 
      System.out.println("not moving ahead!!!");//program stucked here
      bytesRead = is.read(bytearray1,0,bytearray1.length); 
      currentTot = bytesRead; 
      System.out.println("current"+currentTot); 
      do 
      { 
          bytesRead = is.read(bytearray1, currentTot, (bytearray1.length-currentTot)); 
          if(bytesRead &gt;= 0) 
              currentTot += bytesRead; 
          System.out.println("current"+currentTot); 
      } while(bytesRead &gt; -1); 
      System.out.println("outside current"+currentTot);
      bos.write(bytearray1, 0 , currentTot); 
      bos.flush(); 
      //receiving complete
      System.out.println("Receving file completed"); 
      socket.close(); 
  
  } 
  }
  
  
  
  //client side code
  import java.net.*; 
  import java.io.*; 
  
  
   public class client 
  { 
      public static void main (String [] args ) throws IOException 
      { 
          int filesize=1022386; 
          int bytesRead=0; 
          int currentTot = 0; 
          Socket socket = new Socket("localhost",16167); 
          byte [] bytearray = new byte [filesize]; 
          InputStream is = socket.getInputStream(); 
          FileOutputStream fos = new FileOutputStream("/Users/abhishek/desktop/copy.txt"); 
          BufferedOutputStream bos = new BufferedOutputStream(fos); 
          bytesRead = is.read(bytearray,0,bytearray.length); 
          currentTot = bytesRead; 
          do 
          { 
              bytesRead = is.read(bytearray, currentTot, (bytearray.length-currentTot)); 
              if(bytesRead &gt;= 0) 
                  currentTot += bytesRead; 
          } while(bytesRead &gt; -1); 
          System.out.println("current"+currentTot);
          bos.write(bytearray, 0 , currentTot); 
          bos.flush(); 
          bos.close(); 
          System.out.println("receiving first file completed!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
  
          //sending file
          System.out.println("sending second file started!");
          File transferFile = new File ("/Users/abhishek/desktop/file2.txt"); 
          byte [] bytearray2 = new byte [(int)transferFile.length()]; 
          FileInputStream fin = new FileInputStream(transferFile); 
          BufferedInputStream bin = new BufferedInputStream(fin); 
          bin.read(bytearray2,0,bytearray2.length); 
          OutputStream os = socket.getOutputStream();
          os.flush();
          os.write(bytearray2,0,bytearray2.length); 
          os.flush();
          System.out.println("sending second file completed!");
          //sending complete
  
  
          socket.close();
      } 
  }
  </code></pre>