?
Solved

FTP Socket connections

Posted on 1998-10-12
5
Medium Priority
?
292 Views
Last Modified: 2008-02-01
Hi,
    I'm developing an application that interacts with  Microsoft FTP Server (With IIS 4.0). If I try to open a scoket connection on port 21 and send in logon commands  it allows me to logon. And if I try to execute FTP commands like LIST, STOR, RETR it is returning a response 425 can't open data connection.  I understood there has to be separate  connection for data other than the one with which we logged. I'll appreciate if any one can help me in sending and retrieving the file to/from FTP Server using Java.

Thanks in advance
Hemadri
0
Comment
Question by:hemadri
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 5

Accepted Solution

by:
msmolyak earned 300 total points
ID: 1225671
This is a working implementation of FTP client. I hope it is helpful in your pursuit.

/* FTPConnection.java
   Copyright (C) 1997 Shugo Maeda */

import java.io.*;
import java.util.*;
import java.net.*;

public class FTPConnection {

    public static boolean socketGetLocalAddressHasBug;
    static {
      //String classVersionString =
      //    System.getProperty("java.class.version");
      //double classVersion =
      //    Double.valueOf(classVersionString).doubleValue();
      socketGetLocalAddressHasBug = // classVersion < ??.? &&
          (System.getProperty("os.arch").equals("x86") &&
          !System.getProperty("os.name").startsWith("Win")) ||
          (System.getProperty("java.vendor").startsWith("Netscape"));
    }

    protected static final byte[] CRLF = {'\r', '\n'};
    protected static final String lineSeparator =
      System.getProperty("line.separator");
    protected static final byte[] lineSeparatorByteArray =
      lineSeparator.getBytes();

    protected Socket socket;
    protected BufferedReader sockIn;
    protected OutputStream sockOut;

    protected boolean passive = false;

    private OutputStream logOut = new OutputStream() {
      public void write(int b) {
      }
    };

    public FTPConnection(String host, int port)
      throws FTPException, UnknownHostException, IOException {

      InputStreamReader tmp;

      socket = new Socket(host, port);
      tmp = new InputStreamReader(socket.getInputStream());
      sockIn = new BufferedReader(tmp);
      sockOut = socket.getOutputStream();
      checkResponse();
    }

    public FTPConnection(String host)
      throws FTPException, UnknownHostException, IOException {

      this(host, 21);
    }

    public FTPConnection(String host, String user, String password)
      throws FTPException, UnknownHostException, IOException {

      this(host);
      login(user, password);
    }

    public FTPConnection(InetAddress address, int port)
      throws FTPException, IOException {

      InputStreamReader tmp;

      socket = new Socket(address, port);
      tmp = new InputStreamReader(socket.getInputStream());
      sockIn = new BufferedReader(tmp);
      sockOut = socket.getOutputStream();
      checkResponse();
    }

    public FTPConnection(InetAddress address)
      throws FTPException, IOException {

      this(address, 21);
    }

    public FTPConnection(InetAddress address, String user, String password)
      throws FTPException, IOException {

      this(address);
      login(user, password);
    }

    protected String getLocalAddress(Socket socket) {
      InetAddress address = socket.getLocalAddress();
      if (socketGetLocalAddressHasBug) {
          byte[] addr = address.getAddress();
          StringBuffer buff = new StringBuffer();
          for (int i = addr.length - 1; i >= 0; i--) {
            int n = (int) addr[i];
            if (n < 0) n += 256;
            if (i != addr.length - 1)
                buff.append(',');
            buff.append(String.valueOf(n));
          }
          return new String(buff);
      } else {
          return address.getHostAddress().replace('.', ',');
      }
    }

    public void setPassive(boolean passive) {
      this.passive = passive;
    }

    public boolean isPassive() {
      return passive;
    }

    public void setLogOutputStream(OutputStream out) {
      logOut = out;
    }

    public void close() throws IOException {
      synchronized (socket) {
          socket.close();
      }
    }

    protected String getLines() throws IOException {
      StringBuffer buff;
      String line;

      synchronized (socket) {
          line = sockIn.readLine();
          logOut.write(line.getBytes());
          logOut.write(lineSeparatorByteArray);
          buff = new StringBuffer(line);
          if (line.length() > 3 && line.charAt(3) == '-') {
            String code = line.substring(0, 3);
            do {
                line = sockIn.readLine();
                buff.append(lineSeparator).append(line);
            } while (line.length() <= 3
                   || !line.substring(0, 3).equals(code)
                   || line.charAt(3) == '-');
          }
          buff.append(lineSeparator);
      }
      return new String(buff);
    }

    protected String getResponse()
      throws FTPException, IOException {

      String response = getLines();
      char ch = response.charAt(0);
      if (ch == '1' || ch == '2' || ch == '3')
          return response;
      else
          throw new FTPException(response);
    }

    protected void checkResponse()
      throws FTPException, IOException {

      String response = getLines();
      if (response.charAt(0) != '2')
          throw new FTPException(response);
    }

    public String sendCommand(String cmd)
      throws FTPException, IOException {

      synchronized (socket) {
          sockOut.write(cmd.getBytes());
          sockOut.write(CRLF);
          logOut.write(cmd.getBytes());
          logOut.write(lineSeparatorByteArray);
          return getResponse();
      }
    }

    public void voidCommand(String cmd)
      throws FTPException, IOException {

      synchronized (socket) {
          sockOut.write(cmd.getBytes());
          sockOut.write(CRLF);
          logOut.write(cmd.getBytes());
          logOut.write(lineSeparatorByteArray);
          checkResponse();
      }
    }

    protected void sendPort(String host, int port)
      throws FTPException, IOException {

      String cmd;
      StringBuffer buff = new StringBuffer("PORT ");
      Vector tmp = new Vector();

      tmp.addElement(host);
      tmp.addElement(String.valueOf(port / 256));
      tmp.addElement(String.valueOf(port % 256));
      Enumeration enum = tmp.elements();
      if (enum.hasMoreElements())
          buff.append((String) enum.nextElement());
      while (enum.hasMoreElements()) {
          buff.append(",");
          buff.append((String) enum.nextElement());
      }
      cmd = new String(buff);
      voidCommand(cmd);
    }

    protected ServerSocket makePort()
      throws FTPException, IOException {

      ServerSocket sock = new ServerSocket(0);
      String localAddress = getLocalAddress(socket);
      sendPort(localAddress, sock.getLocalPort());
      return sock;
    }

    class PortInfo {
      public String host;
      public int port;
      public PortInfo(String host, int port) {
          this.host = host;
          this.port = port;
      }
    }

    protected PortInfo parse227(String str)
      throws FTPException {

      String tmp, host;
      int left, right, i1, i2, div, mod, port;

      if (str.length() < 3 || !str.substring(0, 3).equals("227"))
          new FTPException(str);
      left = str.indexOf("(") + 1;
      right = str.indexOf(")");
      if (left == -1 || right == -1)
          new FTPException(str);
      tmp = str.substring(left, right);
      i1 = tmp.lastIndexOf(",");
      if (i1 == -1)
          new FTPException(str);
      mod = Integer.parseInt(tmp.substring(i1 + 1));
      i2 = tmp.substring(0, i1).lastIndexOf(",");
      if (i2 == -1)
          new FTPException(str);
      div = Integer.parseInt(tmp.substring(i2 + 1, i1));

      host = tmp.substring(0, i2).replace(',', '.');
      port = (div << 8) + mod;

      return new PortInfo(host, port);
    }

    public Socket transferCommand(String cmd)
      throws FTPException, IOException {

      Socket result;
      String response;

      if (passive) {
          synchronized (socket) {
            PortInfo info = parse227(sendCommand("PASV"));
            result = new Socket(info.host, info.port);
            response = sendCommand(cmd);
          }
          if (response.charAt(0) != '1')
            new FTPException(response);
      } else {
          ServerSocket sock;
          synchronized (socket) {
            sock = makePort();
            response = sendCommand(cmd);
          }
          if (response.charAt(0) != '1')
            new FTPException(response);
          result = sock.accept();
      }
      return result;
    }

    public void restart(long count) throws FTPException, IOException {
      String response = sendCommand("REST " + count);
      if (response.length() < 3 ||
          !response.substring(0, 3).equals("350"))
          new FTPException(response);
    }

    public Socket resumeTransfer(String cmd, long count)
      throws FTPException, IOException {

      Socket result;
      String response;

      if (passive) {
          synchronized (socket) {
            PortInfo info = parse227(sendCommand("PASV"));
            result = new Socket(info.host, info.port);
            restart(count);
            response = sendCommand(cmd);
          }
          if (response.charAt(0) != '1')
            new FTPException(response);
      } else {
          ServerSocket sock;
          synchronized (socket) {
            sock = makePort();
            restart(count);
            response = sendCommand(cmd);
          }
          if (response.charAt(0) != '1')
            new FTPException(response);
          result = sock.accept();
      }
      return result;
    }

    public void retrieveBinary(String cmd, int blocksize,
                         ByteArrayCallback callback)
      throws FTPException, IOException {

      Socket connection;
      InputStream in;
      int size;
      byte[] block = new byte[blocksize];

      voidCommand("TYPE I");
      connection = transferCommand(cmd);
      try {
          in = connection.getInputStream();
          while ((size = in.read(block)) > 0) {
            if (size < blocksize) {
                byte[] b = new byte[size];
                System.arraycopy(block, 0, b, 0, size);
                callback.call(b);
            } else {
                callback.call(block);
            }
          }
      } finally {
          connection.close();
          checkResponse();
      }
    }

    public void resumeRetrieveBinary(String cmd, int blocksize, long count,
                             ByteArrayCallback callback)
      throws FTPException, IOException {

      Socket connection;
      InputStream in;
      int size;
      byte[] block = new byte[blocksize];

      voidCommand("TYPE I");
      connection = resumeTransfer(cmd, count);
      try {
          in = connection.getInputStream();
          while ((size = in.read(block)) > 0) {
            if (size < blocksize) {
                byte[] b = new byte[size];
                System.arraycopy(block, 0, b, 0, size);
                callback.call(b);
            } else {
                callback.call(block);
            }
          }
      } finally {
          connection.close();
          checkResponse();
      }
    }

    public void retrieveLines(String cmd, StringCallback callback)
      throws FTPException, IOException {

      Socket connection;
      InputStreamReader isr;
      BufferedReader br;
      String line;

      voidCommand("TYPE A");
      connection = transferCommand(cmd);
      try {
          isr = new InputStreamReader(connection.getInputStream());
          br = new BufferedReader(isr);
          while ((line = br.readLine()) != null) {
            callback.call(line);
          }
      }
      catch(SocketException e) {
          e.printStackTrace();
      } finally {
          connection.close();
          checkResponse();
      }
    }

    public void storeBinary(String cmd, InputStream in, int blocksize)
      throws FTPException, IOException {

      Socket connection;
      OutputStream out;
      int size;
      byte[] block = new byte[blocksize];

      voidCommand("TYPE I");
      connection = transferCommand(cmd);
      try {
          out = connection.getOutputStream();
          while ((size = in.read(block)) > 0) {
            if (size < blocksize) {
                byte[] b = new byte[size];
                System.arraycopy(block, 0, b, 0, size);
                out.write(b);
            } else {
                out.write(block);
            }
          }
      } finally {
          connection.close();
          checkResponse();
      }
    }

    public void storeBinary(String cmd, InputStream in, int blocksize,
                      ByteArrayCallback callback)
      throws FTPException, IOException {

      Socket connection;
      OutputStream out;
      int size;
      byte[] block = new byte[blocksize];

      voidCommand("TYPE I");
      connection = transferCommand(cmd);
      try {
          out = connection.getOutputStream();
          while ((size = in.read(block)) > 0) {
            if (size < blocksize) {
                byte[] b = new byte[size];
                System.arraycopy(block, 0, b, 0, size);
                out.write(b);
                callback.call(b);
            } else {
                out.write(block);
                callback.call(block);
            }
          }
      } finally {
          connection.close();
          checkResponse();
      }
    }

    public void resumeStoreBinary(String cmd, InputStream in,
                          int blocksize, long count)
      throws FTPException, IOException {

      Socket connection;
      OutputStream out;
      int size;
      byte[] block = new byte[blocksize];

      voidCommand("TYPE I");
      connection = resumeTransfer(cmd, count);
      try {
          out = connection.getOutputStream();
          while ((size = in.read(block)) > 0) {
            if (size < blocksize) {
                byte[] b = new byte[size];
                System.arraycopy(block, 0, b, 0, size);
                out.write(b);
            } else {
                out.write(block);
            }
          }
      } finally {
          connection.close();
          checkResponse();
      }
    }

    public void storeBinary(String cmd, InputStream in,
                      int blocksize, long count,
                      ByteArrayCallback callback)
      throws FTPException, IOException {

      Socket connection;
      OutputStream out;
      int size;
      byte[] block = new byte[blocksize];

      voidCommand("TYPE I");
      connection = resumeTransfer(cmd, count);
      try {
          out = connection.getOutputStream();
          while ((size = in.read(block)) > 0) {
            if (size < blocksize) {
                byte[] b = new byte[size];
                System.arraycopy(block, 0, b, 0, size);
                out.write(b);
                callback.call(b);
            } else {
                out.write(block);
                callback.call(block);
            }
          }
      } finally {
          connection.close();
          checkResponse();
      }
    }

    public void storeLines(String cmd, Reader reader)
      throws FTPException, IOException {

      Socket connection;
      BufferedReader br;
      OutputStream out;
      String line;

      voidCommand("TYPE I");
      connection = transferCommand(cmd);
      try {
          out = connection.getOutputStream();
          br = new BufferedReader(reader);
          while ((line = br.readLine()) != null) {
            out.write(line.getBytes());
            out.write(CRLF);
          }
      } finally {
          connection.close();
          checkResponse();
      }
    }

    public void storeLines(String cmd, Reader reader,
                     StringCallback callback)
      throws FTPException, IOException {

      Socket connection;
      BufferedReader br;
      OutputStream out;
      String line;

      voidCommand("TYPE I");
      connection = transferCommand(cmd);
      try {
          out = connection.getOutputStream();
          br = new BufferedReader(reader);
          while ((line = br.readLine()) != null) {
            out.write(line.getBytes());
            out.write(CRLF);
            callback.call(line);
          }
      } finally {
          connection.close();
          checkResponse();
      }
    }

    public void abort()
      throws FTPException, IOException {

      String response;
      byte[] cmd = "ABOR".getBytes();

      sockOut.write(cmd);
      sockOut.write(CRLF);
      logOut.write(cmd);
      logOut.write(lineSeparatorByteArray);
      response = getLines();
      if (response.length() < 3 ||
          (!response.substring(0, 3).equals("426") &&
           !response.substring(0, 3).equals("226")))
          new FTPException(response);
    }

    public synchronized void login(String user, String password)
      throws FTPException, IOException {

      String response = sendCommand("USER " + user);
      if (response.charAt(0) == '3')
          response = sendCommand("PASS " + password);
      if (response.charAt(0) != '2')
          new FTPException(response);
    }

    public void quit() throws FTPException, IOException {
      voidCommand("QUIT");
    }

    public Vector nlst()
      throws FTPException, IOException {

      final Vector result = new Vector();

      retrieveLines("NLST", new StringCallback() {
          public void call(String line) {
            result.addElement(line);
          }
      });

      return result;
    }

    public void list(StringCallback callback)
      throws FTPException, IOException {

      retrieveLines("LIST", callback);
    }

    public void list(String arg, StringCallback callback)
      throws FTPException, IOException {

      retrieveLines("LIST " + arg, callback);
    }

    public void renameFile(String oldName, String newName)
      throws FTPException, IOException {

      String response = sendCommand("RNFR " + oldName);

      if (response.charAt(0) != '3')
          throw new FTPException(response);
      voidCommand("RNTO " + newName);
    }

    public void deleteFile(String fileName)
      throws FTPException, IOException {

      String response = sendCommand("DELE " + fileName);

      if (response.length() < 3
          || !response.substring(0, 3).equals("250"))
          new FTPException(response);
    }

    public void changeDirectory(String directory)
      throws FTPException, IOException {

      voidCommand("CWD " + directory);
    }

    public void changeDirectoryUp()
      throws FTPException, IOException {

      voidCommand("CDUP");
    }

    public long getFileSize(String fileName)
      throws FTPException, IOException {

      voidCommand("TYPE I");
      String response = sendCommand("SIZE " + fileName);
      if (response.length() < 3
          || !response.substring(0, 3).equals("213"))
          new FTPException(response);
      return Long.parseLong(response.substring(3).trim());
    }

    protected String parse257(String str)
      throws FTPException {

      StringBuffer buff = new StringBuffer("");

      if (str.length() < 3 || !str.substring(0, 3).equals("257"))
          new FTPException(str);
      if (str.length() < 5 || !str.substring(3, 5).equals(" \""))
          return "";
      for (int i = 5; i < str.length(); i++) {
          char ch = str.charAt(i);
          if (ch == '"') {
            if (i > str.length() - 1
                || str.charAt(i + 1) != '"')
                break;
            i++;
          }
          buff.append(ch);
      }
      return new String(buff);
    }

    public String getCurrentDirectory()
      throws FTPException, IOException {

      return parse257(sendCommand("PWD"));
    }

    public String createDirectory(String directory)
      throws FTPException, IOException {

      return parse257(sendCommand("MKD " + directory));
    }

    public void deleteDirectory(String directory)
      throws FTPException, IOException {

      voidCommand("RMD " + directory);
    }
}

0
 

Author Comment

by:hemadri
ID: 1225672
I'm not able to find FTPException, StringCallback and ByteArraycallback. If have any other source let me know

Thanks
Hemadri
0
 
LVL 6

Expert Comment

by:gadio
ID: 1225673
hemadri, there is another way of dealing with ftp connection. In the sun packages there is an ftp client that you can use. I have attached a sample code that does that. This direction may address your problem with a minimum writing of code. Tell me if it may solve your problem, I'll paste the class interface.


      ------ file: ftp_utility.java ------

      import sun.net.*;
      import sun.net.ftp.*;
      import java.io.*;


      public class ftp_utility {

          public ftp_utility () {
              FtpClient fcMyFtp = new FtpClient();

              try {
                  fcMyFtp.openServer("servername");
                  fcMyFtp.login("gadio","mypassword");
                  TelnetInputStream tisList = fcMyFtp.list();
                  int ch;
                  while( (ch = tisList.read()) != -1 ) System.out.print((char)ch);
                  fcMyFtp.cd("/pub");
                  // ... other stuff.
              } catch( IOException e ) {
                  e.printStackTrace();
              }
          }

          public static void main(String args[]) {
              ftp_utility f = new ftp_utility();
          }
      }

      ------
0
 
LVL 5

Expert Comment

by:msmolyak
ID: 1225674
Here is the rest:


/*
 * FTPException.java
 */
public class FTPException extends Exception {
    /**
     * Constructs a new FTPException with the specified detail
     * message as to why the connect error occurred.
     * A detail message is a String that gives a specific
     * description of this error.
     * @param msg the detail message
     */
    public FTPException(String msg) {
      super(msg);
    }

    /**
     * Construct a new FTPException with no detailed message.
     */
    public FTPException() {
      super();
    }
}

/* ByteArrayCallback.java
   Copyright (C) 1997 Shugo Maeda */

public interface ByteArrayCallback {
    public void call(byte[] block);
}


/* StringCallback.java
   Copyright (C) 1997 Shugo Maeda */

public interface StringCallback {
    public void call(String line);
}





0
 

Author Comment

by:hemadri
ID: 1225675
gadio, I found u'r information useful. But I couldn't find documentation for sun.net.* classes. With these classes I'm able to retireve a file from but I'm not able to send a file. I tried somehow but the file is creating with 0 bytes at server side. what is the workaround for it

thanx
Hemadri
0

Featured Post

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses
Course of the Month8 days, 15 hours left to enroll

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question