3 Replies Latest reply on Dec 28, 2005 4:38 AM by 807597

    error while ftp: Software caused connection abort: socket write error

    807597
      Hello,
      I am trying to ftp using the Java ftp libs, and when that failed, the Commons Net FTPClient.
      I am able to login and the empty file is created. However for both, I get the following SocketException. Any help appreciated. The code is mostly taken from the javadocs for Commons Net.
      thanks,
      Anil
      -------------------

      Connected to juwo.com.
      230 User juwo logged in.
      java.net.SocketException: Software caused connection abort: socket write error
           at java.net.SocketOutputStream.socketWrite0(Native Method)
           at java.net.SocketOutputStream.socketWrite(Unknown Source)
           at java.net.SocketOutputStream.write(Unknown Source)
           at org.apache.commons.net.io.SocketOutputStream.write(SocketOutputStream.java:69)
           at MyFTP.put3(MyFTP.java:79)
           at MyFTP.main(MyFTP.java:105)


      ============= CODE ==================
           static void put3(String server, String user, String passwd)
                     throws IOException {
                boolean error = false;
                FTPClient ftp = null;
                try {
                     int reply;
                     ftp = new FTPClient();
                     ftp.connect(server);
                     ftp.login(user, passwd);
      
                     System.out.println("Connected to " + server + ".");
                     System.out.print(ftp.getReplyString());
      
                     // After connection attempt, you should check the reply code to
                     // verify
                     // success.
                     reply = ftp.getReplyCode();
      
                     if (!FTPReply.isPositiveCompletion(reply)) {
                          ftp.disconnect();
                          System.err.println("FTP server refused connection.");
                          System.exit(1);
                     }
                     ftp.setFileType(FTP.IMAGE_FILE_TYPE);
                     ftp.cwd("/wwwroot/nodepad");
                     OutputStream out = ftp.appendFileStream("anilsvoice.spx");
                     File src = new File("C:\\anilsvoice.spx");
                     InputStream in = new FileInputStream(src);
      
                     // Transfer bytes from in to out
                     byte[] buf = new byte[1024];
                     int len;
                     while ((len = in.read(buf)) > 0) {
                          out.write(buf, 0, len);
                     }
                     in.close();
                     out.close();
                     ftp.logout();
                } catch (IOException e) {
                     error = true;
                     e.printStackTrace();
                } finally {
                     if (ftp.isConnected()) {
                          try {
                               ftp.disconnect();
                          } catch (IOException ioe) {
                               // do nothing
                          }
                     }
                     System.exit(error ? 1 : 0);
                }
      
           }
        • 1. Re: error while ftp: Software caused connection abort: socket write error
          800384
          Found it. Had to read down a bit, but I found it. I can see how you might have missed it.

          The setup you have is:

          1. Open connection
          2. Login
          3. Change Directory
          4. Create file
          5. Close streams
          6. Logout

          Look at #5-6, they are in REVERSE ORDER.

          You need to send the ftp.logout() BEFORE you close the in/out streams. You CAN close the stream 'in' because that is connected to a local file, not the socket. However, when you create the stream 'out' you are switching that referenced object to the new 'out' object. When the ftp.logout() method is called, it is trying to write to the now-closed 'out' stream and NOT it's original stream which was deleted when you created the 'out' stream with that object as it's value (in Java, GC does this automatically, in order to save memory).

          Remember, in Java, you typically won't have two objects of the same thing.

          Example:
          Object obj1;
          Object obj2;

          // Currently there are two INDEPENDENT objects in memory.

          obj1 = obj2; /** Now, we have assigned the value of obj2 to obj1, the Java GC will collect obj2 and delete it from memory, all references to obj2 will now reference obj1 and it's state. **/
          • 2. Re: error while ftp: Software caused connection abort: socket write error
            800384
            static void put3(String server, String user, String passwd)
                           throws IOException {
                      boolean error = false;
                      FTPClient ftp = null;
                      try {
                           int reply;
                           ftp = new FTPClient();
                           ftp.connect(server);
                           ftp.login(user, passwd);
             
                           System.out.println("Connected to " + server + ".");
                           System.out.print(ftp.getReplyString());
             
                           // After connection attempt, you should check the reply code to
                           // verify
                           // success.
                           reply = ftp.getReplyCode();
             
                           if (!FTPReply.isPositiveCompletion(reply)) {
                                ftp.disconnect();
                                System.err.println("FTP server refused connection.");
                                System.exit(1);
                           }
                           ftp.setFileType(FTP.IMAGE_FILE_TYPE);
                           ftp.cwd("/wwwroot/nodepad");
                           OutputStream out = ftp.appendFileStream("anilsvoice.spx");
                           File src = new File("C:\\anilsvoice.spx");
                           InputStream in = new FileInputStream(src);
             
                           // Transfer bytes from in to out
                           byte[] buf = new byte[1024];
                           int len;
                           while ((len = in.read(buf)) > 0) {
                                out.write(buf, 0, len);
                           }
                           in.close();
                                                                            ftp.logout(); // THIS HAS BEEN CHANGED
                           out.close(); // THIS HAS BEEN CHANGED
                           
                      } catch (IOException e) {
                           error = true;
                           e.printStackTrace();
                      } finally {
                           if (ftp.isConnected()) {
                                try {
                                     ftp.disconnect();
                                } catch (IOException ioe) {
                                     // do nothing
                                }
                           }
                           System.exit(error ? 1 : 0);
                      }
             
                 }
            • 3. Re: error while ftp: Software caused connection abort: socket write error
              807597
              No, that was not the problem. logout will not produce that error on a closed stream. The problem was external - my web hosting provider's disk space quota was full! :)
              I ran my original program and it worked without errors and so did your code. Thanks and sorry for any inconvenience!
              -
              Anil

              Found it. Had to read down a bit, but I found it. I
              can see how you might have missed it.
              Look at #5-6, they are in REVERSE ORDER.

              You need to send the ftp.logout() BEFORE you
              close the in/out streams. You CAN close the stream
              'in' because that is connected to a local file, not
              the socket. However, when you create the stream 'out'
              you are switching that referenced object to the new
              'out' object. When the ftp.logout() method is called,
              it is trying to write to the now-closed 'out' stream
              and NOT it's original stream which was deleted when
              you created the 'out' stream with that object as it's