6 Replies Latest reply: Sep 27, 2010 3:19 AM by mlk RSS

    FTP client with SocketConnection

    762917
      Hi,

      i am trying to develop an ftp client program, but i have problems with the data connection part. The client needs two type of connections: control and data connection. The control part its ok, i can log in to the server and send some commands to it but when i try to open the data connection i get this error code:

      425 Can´t open data connection.

      i am doing this locally on the same computer, and i dont have some firewall configured, the path between the client and server is clean, both are in the same computer.

      this is my code:
      remoteFTPServerAddress = "10.3.11.104";
      
      public void run() {
            try {
              sc = (SocketConnection)
                 Connector.open("socket://"+remoteFTPServerAddress+":21");
              is = sc.openInputStream();
              dis = new DataInputStream(is);
              os = sc.openOutputStream();
              dos = new DataOutputStream(os);
      
              StringBuffer sb = new StringBuffer();
              int c = 0;
              while (((c = dis.read()) != '\n') && (c != -1)) {
                 sb.append((char) c);
              }        
              si.setText(sb.toString());
      
              dos.write(("USER joseluis" + "\r\n").getBytes());
              dos.flush();
      
              sb.delete(0,sb.length());
              c = 0;
              while (((c = dis.read()) != '\n') && (c != -1)) {
                 sb.append((char) c);
              }
              s2.setText(sb.toString());
      
              dos.write(("PASS joseluis" + "\r\n").getBytes());
              dos.flush();
      
              sb.delete(0,sb.length());
              c = 0;
              while (((c = dis.read()) != '\n') && (c != -1)) {
                 sb.append((char) c);
              }
              s3.setText(sb.toString());
      
              dos.write(("PWD" + "\r\n").getBytes());
              dos.flush();
      
              sb.delete(0,sb.length());
              c = 0;
              while (((c = dis.read()) != '\n') && (c != -1)) {
                 sb.append((char) c);
              }
              s4.setText(sb.toString());
      
              dos.write(("TYPE I" + "\r\n").getBytes());
              dos.flush();
      
              sb.delete(0,sb.length());
              c = 0;
              while (((c = dis.read()) != '\n') && (c != -1)) {
                 sb.append((char) c);
              }
              s5.setText(sb.toString());
      
              //dos.write(("PORT 10,3,11,104,195,88" + "\r\n").getBytes()); i have tried active mode and fails too.
              dos.write(("PASV" + "\r\n").getBytes());
              dos.flush();        
      
              sb.delete(0,sb.length());
              c = 0;
              while (((c = dis.read()) != '\n') && (c != -1)) {
                 sb.append((char) c);
              }        
              s6.setText(sb.toString());
      
              dos.write(("LIST" + "\r\n").getBytes()); // Here is the problem
              dos.flush();       
      
              sb.delete(0,sb.length());
              c = 0;
              while (((c = dis.read()) != '\n') && (c != -1)) {
                 System.out.println("Bytes: "+(char) c);
                 sb.append((char) c);
              }
              s7.setText(sb.toString());
            } catch(IOException e) {
               Alert a = new Alert("FTPClient", "Cannot connect to server. Ping the server to make sure it is running...", null,  AlertType.ERROR);
               a.setTimeout(Alert.FOREVER);
               display.setCurrent(a);
            } finally {
               try {
                 if(is != null) {
                   is.close();
                 }
                 if(sc != null) {
                   sc.close();
                 }
               } catch(IOException e) {
                 e.printStackTrace();
               }
            }
         }
      Thanx.