Hi Rory
Below is the code.

FTPClient ftp1 = null;
 MySocketFactory f1 = new MySocketFactory("9.182.74.131", 1088);
 MySocketFactory f2 = new MySocketFactory("9.182.149.155", 1080);

 try {
  ftp1 = new FTPClient();
  ftp1.setSocketFactory(f1);

  ftp1.addProtocolCommandListener(new PrintCommandListener(new
PrintWriter(new BufferedWriter(new FileWriter("ftpLogs.txt")))));
  ftp1.connect(hostName1);
  System.out.println("ASK1 Connected to " + hostName1 + ".");
  int reply1 = ftp1.getReplyCode();

  if (!FTPReply.isPositiveCompletion(reply1))
           {
               ftp1.disconnect();
               System.out.println("ASK1 FTP server refused connection.");
               System.exit(1);
           }
           if (ftp1.login(username1, password1)){
            System.out.println("ASK1 successful login, Remote system is " +
ftp1.getSystemName());
            System.out.println("ASK1 get status : "+ftp1.getStatus());
            ftp1.enterLocalPassiveMode();

            System.out.println("ASK1 listing names for ftp1 first time");
            String[] ftp1_l1 = ftp1.listNames();
            System.out.println("ASK1 got list output");
           }


           ftp1.setSocketFactory(f2);
           System.out.println("ASK1 listing names for ftp1 second time");
           String[] ftp1_l2 = ftp1.listNames();
This is the SocketFactory code.

package com.ibm.ask.general;

import java.io.IOException;
import java.net.Authenticator;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;

import org.apache.commons.net.SocketFactory;

public class MySocketFactory implements SocketFactory
{
private String socksProxyHost = null;
private int socksProxyPort = 1080;
private String socksUserName = null;
private String socksPassword = null;

private MyAuthenticator authObject = null;

public MySocketFactory(){
}

public MySocketFactory(String socksProxyHost, int socksProxyPort){
 this.socksProxyHost = socksProxyHost;
 this.socksProxyPort = socksProxyPort;
}

public MySocketFactory(String socksProxyHost, int socksProxyPort, String
socksUserName, String socksPassword){
 this.socksProxyHost = socksProxyHost;
 this.socksProxyPort = socksProxyPort;
 this.socksUserName = socksUserName;
 this.socksPassword = socksPassword;
 if(socksUserName != null && socksPassword!= null){
  authObject = new MyAuthenticator(socksUserName, socksPassword);
  Authenticator.setDefault(authObject);
 }
}

   public Socket createSocket(String host, int port) throws
UnknownHostException, IOException
   {
       if(socksProxyHost == null){
        System.out.println("1.1 creating socket");
        return new Socket(host, port);
       }
       else{
        System.out.println("1.2 creating socket");
        Socket socket = new Socket(new Proxy(Proxy.Type.SOCKS, new
InetSocketAddress(socksProxyHost, socksProxyPort)));
        socket.connect(new InetSocketAddress(host, port));
        return socket;
       }
   }

   public Socket createSocket(InetAddress address, int port) throws
IOException
   {
    if(socksProxyHost == null){
     System.out.println("2.1 creating socket");
     return new Socket(address, port);
    }
    else{
     System.out.println("2.2 creating socket");
     Socket socket = new Socket(new Proxy(Proxy.Type.SOCKS, new
InetSocketAddress(socksProxyHost, socksProxyPort)));
        socket.connect(new InetSocketAddress(address, port));
        return socket;
    }
   }

   public Socket createSocket(String host, int port, InetAddress localAddr,
int localPort) throws UnknownHostException, IOException
   {
    if(socksProxyHost == null){
     System.out.println("3.1 creating socket");
     return new Socket(host, port, localAddr, localPort);
    }
    else{
     System.out.println("3.2 creating socket");
     Socket socket = new Socket(new Proxy(Proxy.Type.SOCKS, new
InetSocketAddress(socksProxyHost, socksProxyPort)));
        socket.connect(new InetSocketAddress(host, port));
        socket.bind(new InetSocketAddress(localAddr, localPort));
        return socket;
    }
   }

   public Socket createSocket(InetAddress address, int port,InetAddress
localAddr, int localPort) throws IOException
   {
    if(socksProxyHost == null){
     System.out.println("4.1 creating socket");
     return new Socket(address, port, localAddr, localPort);
    }
    else{
     System.out.println("4.2 creating socket");
     Socket socket = new Socket(new Proxy(Proxy.Type.SOCKS, new
InetSocketAddress(socksProxyHost, socksProxyPort)));
        socket.connect(new InetSocketAddress(address, port));
        socket.bind(new InetSocketAddress(localAddr, localPort));
        return socket;
    }
   }

   public ServerSocket createServerSocket(int port) throws IOException
   {
       return new ServerSocket(port);
   }

   public ServerSocket createServerSocket(int port, int backlog)
   throws IOException
   {
       return new ServerSocket(port, backlog);
   }

   public ServerSocket createServerSocket(int port, int backlog,
                                          InetAddress bindAddr)
   throws IOException
   {
       return new ServerSocket(port, backlog, bindAddr);
   }

}


Thanks
Sarath

Reply via email to