title: readUTF not reading from second time in socket file transfer JAVA
body: <p>i am trying to transfer files via socket. I've used only a single socket for communication( not according to FTP protocol i guess). The following code will transfer the <strong>first</strong> file successfully but is <strong>not able to tra nsfer second file</strong> as the <strong>filename doesn't change but the server gets the read bytes of the new data file</strong>. I think the problem is of the readUTF and writeUTF.</p>
  
  <p>Here is my server side code. <strong>Remember this accepts the file.Not send file.</strong></p>
  
  <pre><code>public int listenPort() throws IOException{
      System.out.println("LISTENING");
              try{
                  //this.dis = new DataInputStream(this.socketClient.getInputStream());
                  if( this.dis.available() != 0 ){
  
                  String filename = this.dis.readUTF();
                  this.fos = new FileOutputStream("/home/ankit07/" + filename);
  
                  int bytesRead = (int) IOUtils.copyLarge(this.dis,this.fos); //no of bytes copied
                  return bytesRead;
  
                  }else{
                      return 0;
                  }
              }finally{
  
              }
  }
  </code></pre>
  
  <p>Here is my client side. <strong>Remember this side sends the file. Not accept</strong></p>
  
  <pre><code>public void getFile(String filename) throws IOException{
              try{
                  this.file = this.window.file;
                  DataOutputStream dos = new DataOutputStream(this.socketClient.getOutputStream());
                  dos.writeUTF(filename);
                  FileInputStream fis = new FileInputStream(this.file);
                  int readByte = (int) IOUtils.copyLarge(fis, dos);
                  System.out.println("FILE SENT : " + filename + "  Bytes :" + readByte);
  
                  //this.socketClient.close();
              }finally{               
                  //if( this.os!=null)    this.os.close();
                  if( this.window.file != null) this.window.file = null;
                  if( this.file != null) this.file = null;
                  //if( this.socketClient!=null) this.socketClient.close();
              }
  }
  </code></pre>
  
  <p>The file selections are done in other class <strong>window</strong>.</p>
  
  <p>The method to select the file is in the <strong>window class</strong>. This has a public File property to hold the file and then i've called the <strong>getFile(String filename)</strong> to send the file name and to refer to the selected file, the client has File property to refer to the same file.</p>
  
  <pre><code>public void actionPerformed(ActionEvent e) {
      // TODO Auto-generated method stub
      Object src = e.getSource();
  
      if( src instanceof JButton ){   //Browse clicked
          JFileChooser fc = new JFileChooser(); 
          int returnVal = fc.showDialog(null, "SELECT FILE");
          if( returnVal == JFileChooser.APPROVE_OPTION){
                  this.file = fc.getSelectedFile();
                  try {
                      this.sc.getFile(this.file.getName());
                  } catch (IOException e1) {
                      // TODO Auto-generated catch block
                      e1.printStackTrace();
                  }
          }else{
              //unable to select file 
          }
      }
  }
  </code></pre>
  
  <p>Also i am not able to transfer large files like mp3 and video besides my initial problem. It would be helpful if you'd know any solutions.
  Thanks you !!!!</p>