URGENT: FTP Client / Server using RMI

Reply

Join Date: Apr 2006
Posts: 8
Reputation: lwinn213 is an unknown quantity at this point 
Solved Threads: 0
lwinn213 lwinn213 is offline Offline
Newbie Poster

URGENT: FTP Client / Server using RMI

 
0
  #1
Apr 22nd, 2006
Hi All.

I have been given a task where I have been asked to implement a Java RMI version of an SFTP (Simple File Transfer Protocol) Client / Server. It must use the following commands:

list - list files in current directory
cd - change directory
get filename - copy file from server to client
put - upload file from client to server
bye - close connection

In essence, a FTPServer Factory has to be created which i'm guessing would use the rmi registry to handle requests between the client and server.

I have the RMI part figured out, just trying to get the FTP part working...

I decided to use the Java Library from JScape Inet Factory and all classes compile, the Ftp Server is running and waits for a connection on port 1099 the designated default RMI port.

When the Client starts up it asks user to confirm localhost name and then asks user for username and password, these are hardcoded as "user" and "pass" respectively.

The client then begins to log in, the client finds the RMI name through the registry and then trys to log into the FTP server, but everytime it tries to do this it times out as it cannot see the FTP Server. Any ideas??

I have attached code for FTPServer, the Implementation and the Client...any help would be appreciated

Thanks

RMI FTP SERVER

  1.  
  2. package server;
  3. import java.net.*;
  4. import java.net.UnknownHostException;
  5. import java.io.*;
  6. import java.util.*;
  7. import java.lang.Thread;
  8. import java.rmi.*;
  9. import java.rmi.Remote;
  10. import java.rmi.Naming;
  11. import java.rmi.RemoteException;
  12. import java.rmi.server.*;
  13. import com.jscape.inet.ftp.*;
  14. import com.jscape.inet.ftp.FtpException;
  15. import server.*;
  16.  
  17.  
  18. public class RmiFtpServer {
  19.  
  20. //static String hostName = "localhost";
  21. private static String hostName;
  22. private static int hostPort = 1099;
  23. static RmiFtpImpl rmi;
  24.  
  25. public static void main(String[] args) throws RemoteException, FtpException, UnknownHostException {
  26.  
  27. String host;
  28. String port;
  29. String badhost;
  30. String error;
  31.  
  32. //System.setSecurityManager(new RMISecurityManager());
  33.  
  34. try {
  35.  
  36. InetAddress addr = InetAddress.getLocalHost();
  37.  
  38. // Get IP Address
  39. byte[] ipaAddr = addr.getAddress();
  40.  
  41. // Get Host Name
  42. String hostName = addr.getHostName();
  43.  
  44. RmiFtpImpl rmi = new RmiFtpImpl("RmiFtpServer");
  45. System.out.println("RmiFtpServer object created...");
  46. System.out.println("Registering FTP Server with RMI Naming service...");
  47.  
  48. String serverObjectName = "rmi://localhost/RmiFtpServer";
  49. Naming.rebind(serverObjectName, rmi);
  50.  
  51. System.out.println("RmiFtpServer bound in RMI registry");
  52. host = "Bindings Finished. FTP Server hostname is: " + hostName;
  53. System.out.println(host);
  54. port = "Waiting for FTP Client requests on port "+ hostPort +" ...";
  55. System.out.println(port);
  56. }
  57. catch (UnknownHostException uhe) {
  58. badhost = "The host computer name you have specified, "+ hostName +" does not match your real computer name.";
  59. System.out.println(badhost);
  60. //System.exit(1);
  61. }
  62.  
  63. catch (RemoteException re) {
  64. error = "Error starting service :"+re;
  65. System.out.println(error);
  66. //System.exit(1);
  67. }
  68.  
  69. catch (MalformedURLException url) {
  70. System.out.println("RmiFtpServer URL Error: " + url.getMessage());
  71. }
  72.  
  73. }
  74. }

RMI IMPLEMENTATION

package server;
 
import java.io.*;import java.lang.*;
import java.net.*;
import java.net.InetAddress.*;
import java.net.UnknownHostException;
import java.rmi.server.UnicastRemoteObject;
import java.rmi.RemoteException;
import java.util.*;
import com.jscape.inet.ftp.*;
import server.*;
 
public class RmiFtpImpl extends UnicastRemoteObject implements RmiFtp
{ // begin class
 
    private static String hostName;
    private String host, user, password;
    private String name;
    private int port;
    String ftpEx;
    Login log;
 
    public RmiFtpImpl(String s) throws RemoteException
    { // begin rmiftpimpl
        super();
        name = s;
    } // end rmiftpimpl
 
    public boolean doLogin() 
      throws RemoteException, UnknownHostException { // begin do login
 
    System.out.println ("doLogin() method begins here...");
 
    //log = new Login(host, user, password, port);
 
    System.out.println ("validation begins...");
 
    System.out.println ("validation should finish here...");
 
            try { // begin try
          System.out.println ("Attempt to create FTP object...");
 
      InetAddress addr = InetAddress.getLocalHost();
 
      // Get IP Address
      byte[] ipaAddr = addr.getAddress();
 
      // Get Host Name
      String hostName = addr.getHostName();
 
      host = hostName;
      user = "user";
      password = "pass";
 
      Ftp ftpconn = new Ftp(host,user,password);
      System.out.println ("FTP object created...");
      System.out.println(ftpconn);
          //ftpconn.addFtpListener(this);
          ftpconn.connect();
      System.out.println("connection created...");
          //String results = ftpconn.getDirListingAsString();
          //System.out.println(results);
          //ftpconn.disconnect();
          return true;
      } // end try
 
      catch (FtpException ex) { // begin catch
      ftpEx = "FTP Exception: "  + ex.getMessage();
      System.out.println(ftpEx);
      return false;
 
      } // end catch
 
      catch (UnknownHostException uhe) {
      System.out.println("Host Exception Error: " + uhe.getMessage());
      return false;
    }
      }
}

FTP RMI CLIENT

/*
 
package client;
 
/**
 *
 * @author
 */
import com.jscape.inet.ftp.*;
import java.io.*;
import java.net.*; 
import java.net.MalformedURLException;
import java.rmi.*;
import java.rmi.Remote;
import java.rmi.Naming;
import java.rmi.RMISecurityManager;
import java.util.Enumeration;
import server.RmiFtp;
 
public class RmiFtpClient extends FtpAdapter implements FtpListener  {
 
public static String servHost;
private static String hostName;
 
public static void main(String[] args) throws RemoteException {
 
    if (System.getSecurityManager() == null)
    {
    System.setSecurityManager(new RMISecurityManager());
    }
 
    servHost = "localhost";
    if (args.length < 1) {
        System.out.println("Usage: java RmiFtpClient <rmihost>");
        }
 
    else {
        servHost = args[0];
    }
 
    new RmiFtpClient();
}
 
    public RmiFtpClient() {
 
    String host;
    String user;
    String password;
    //int port = 1099;
 
    RmiFtp rmi = null;
 
        try {
 
        InetAddress addr = InetAddress.getLocalHost();
 
        // Get IP Address
            //byte[] ipaAddr = addr.getAddress();
 
        // Get Host Name
        String hostName = addr.getHostName();
 
 
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
            System.out.print("Enter Ftp hostname (e.g. ftp.yourhost.com): " + hostName);
            host = reader.readLine();
            System.out.print("Enter username (e.g. anonymous): ");
            user = reader.readLine();
            System.out.print("Enter password (e.g. password): ");
            password = reader.readLine();
            rmi = (RmiFtp)Naming.lookup("rmi://localhost/RmiFtpServer");
        System.out.println ("Naming OK");
        System.out.println("Logging in to FTP Server...");
            rmi.doLogin();
        System.out.println("did it work?");
            }
 
    catch(RemoteException re) {
            System.out.println("FTP RemoteException Error: " + re.getMessage());
            }
 
    catch(IOException io) {
            System.out.println("FTP IO Error: "  + io.getMessage());
            }
 
        catch(NotBoundException e) {
            e.printStackTrace();
            }
 
    }
}
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,144
Reputation: jwenting is just really nice jwenting is just really nice jwenting is just really nice jwenting is just really nice 
Solved Threads: 212
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: URGENT: FTP Client / Server using RMI

 
0
  #2
Apr 22nd, 2006
what do you mean, it "cannot see the server"?

Log the remote object as it is returned, what are you getting for the endpoint?

If your hostsfile is configured wrong your server might pass your clients an incorrect IP address to use when making calls on the remote object, leading to those calls getting lost and the client reporting a "connection refused" or similar error.
As people are clearly allowed to attack me but I'm not allowed to defend myself, I no longer post to this site.
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 8
Reputation: lwinn213 is an unknown quantity at this point 
Solved Threads: 0
lwinn213 lwinn213 is offline Offline
Newbie Poster

Re: URGENT: FTP Client / Server using RMI

 
0
  #3
Apr 22nd, 2006
I decided to use sockets to run the FTP server and call the methods that the FTP Server and Client using RMI, is this correct?

Well i've been trying to implement this but everytime I try and launch the server I get an IOException error

RmiFtpServer IO Error: Connection refused: connect

if i run the server without calling the Socket outgoing call then it works in a way.

I thought it could have been my firewall refusing the connection, but then if the program is running on 1 machine would the firewall even be used, i didn't think so. So I opened ports 20 and 21 (FTP Ports) but still produced the same exception error.

Any ideas??

RMI SERVER


  1. package server;
  1.  
  2. import java.net.*;
  3.  
  4. import java.net.UnknownHostException;
  5. import java.io.*;
  6. import java.util.*;
  7. import java.lang.Thread;
  8. import java.rmi.*;
  9. import java.rmi.Remote;
  10. import java.rmi.Naming;
  11. import java.rmi.RemoteException;
  12. import java.rmi.server.*;
  13. import com.jscape.inet.ftp.*;
  14. import server.*;
  15. public class RmiFtpServer extends Thread {
  16. //static String hostName = ;
  17. // The port which FTP server will connect to
  18. public static final int SERVER_PORT = 21;
  19.  
  20. // The data port
  21. public static final int SERVER_DATA_PORT = 20;
  22.  
  23. // The server name
  24. public static final String SERVER_HOST = "localhost";
  25.  
  26. // The port this server is listening on
  27. private int portListen;
  28.  
  29.  
  30. // private static int hostPort = 1099;
  31. private static RmiFtpImpl rmi;
  32.  
  33. private static String hostName;
  34. private static String r;
  35. private Socket incoming;
  36. private Socket outgoing;
  37. private int counter;
  38. public static void main(String[] args) throws RemoteException, UnknownHostException {
  39. String host;
  40. String port;
  41. String badhost;
  42. String error;
  43.  
  44. //Socket outgoing;
  45. //BufferedReader in = null;
  46. //PrintWriter out = null;
  47. String line;
  48. int i = 1;
  49. try {
  50. InetAddress addr = InetAddress.getLocalHost();
  51.  
  52. // Get IP Address
  53. byte[] ipAddr = addr.getAddress();
  54.  
  55. // Get hostname
  56. String hostname = addr.getHostName();
  57. System.out.println("begin here...");
  58. RmiFtpImpl rmi = new RmiFtpImpl("RmiFtpServer");
  59. System.out.println("RmiFtpServer object created...");
  60. System.out.println("Registering FTP Server with RMI Naming service...");
  61. String serverObjectName = "rmi://localhost/RmiFtpServer";
  62. Naming.rebind(serverObjectName, rmi);
  63. hostName = SERVER_HOST;
  64. System.out.println("RmiFtpServer bound in RMI registry");
  65. host = "Bindings Finished. FTP Server hostname is : " + hostName;
  66. System.out.println(host);
  67. ServerSocket s = new ServerSocket(21);
  68.  
  69. int portListen = SERVER_PORT;
  70.  
  71. port = "Waiting for FTP Client requests on port "+ portListen + " ...";
  72. System.out.println(port);
  73.  
  74. System.out.println("begin socket...");
  75. Socket outgoing = new Socket(hostName, 20);
  76. //String request="login\r\n";
  77. byte[] b = new byte[1024];
  78. // OutputStream out = outgoing.getOutputStream();
  79. OutputStream out = outgoing.getOutputStream();
  80. BufferedInputStream in = new BufferedInputStream(outgoing.getInputStream());
  81. System.out.println(in.read(b));
  82.  
  83.  
  84. //System.out.println("start client data transfer...");
  85. //String clientData = in.readLine();
  86. //System.out.println("receive data from client " + clientData);
  87.  
  88. //System.out.println("send data to client " + clientData);
  89. //out.println("This is message from server");
  90. //out.flush();
  91.  
  92. //out.write(request.getBytes());
  93.  
  94. //System.out.println(in.read(b));
  95. //for(;;)
  96. //{
  97. Socket incoming = s.accept();
  98. System.out.println(s.getInetAddress());
  99. new RmiFtpServer(incoming,i).start();
  100. i++;
  101. // }
  102. }
  103.  
  104. catch (UnknownHostException uhe) {
  105. badhost = "The host computer name you have specified, "+ hostName +" does not match your real computer name.";
  106. System.out.println(badhost);
  107. //System.exit(1);
  108. }
  109.  
  110. catch (RemoteException re) {
  111. error = "Error starting service :"+re;
  112. System.out.println(error);
  113. //System.exit(1);
  114. }
  115.  
  116. catch (MalformedURLException url) {
  117. System.out.println("RmiFtpServer URL Error: " + url.getMessage());
  118. }
  119. catch (IOException io) {
  120. System.out.println("RmiFtpServer IO Error: " + io.getMessage());
  121. }
  122. }
  123. public RmiFtpServer(Socket income, int c)
  124. {
  125. System.out.println("Entered Constructor");
  126. incoming = income;
  127. counter = c;
  128. }
  129. public void run()
  130. {
  131. int lng, lng1, lng2, i, ip1, ip2, ip = 1, h1;
  132. String a1, a2, di, str1, user="", host, dir;
  133. System.out.println(r);
  134. dir = r;
  135. InetAddress inet;
  136. InetAddress localip;
  137.  
  138. try {
  139. inet = incoming.getInetAddress();
  140. localip = inet.getLocalHost();
  141. host = inet.toString();
  142. h1 = host.indexOf("/qc308_serv");
  143. host = host.substring(h1 + 1);
  144. System.out.println(host);
  145.  
  146. BufferedReader in = new BufferedReader (new InputStreamReader(incoming.getInputStream()));
  147. PrintWriter out = new PrintWriter(incoming.getOutputStream(),true);
  148.  
  149. out.println("SERVER: 220 FTP Server ready.\r");
  150. }
  151. catch (Exception e) {
  152. System.out.println(e);
  153. }
  154.  
  155. }




Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,144
Reputation: jwenting is just really nice jwenting is just really nice jwenting is just really nice jwenting is just really nice 
Solved Threads: 212
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: URGENT: FTP Client / Server using RMI

 
0
  #4
Apr 23rd, 2006
So, the server can't bind the remote.
Hardly surprising if you have no RMI registry running.

And you never even try to find the registry you're trying to register your remote object with, so it's no surprise you can't register it.
As people are clearly allowed to attack me but I'm not allowed to defend myself, I no longer post to this site.
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 8
Reputation: lwinn213 is an unknown quantity at this point 
Solved Threads: 0
lwinn213 lwinn213 is offline Offline
Newbie Poster

Re: URGENT: FTP Client / Server using RMI

 
0
  #5
Apr 23rd, 2006
RMI Registry works fine, I created 2 batch files and get output and no RemoteExceptions, I can attach screen shots if necessary
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 8
Reputation: lwinn213 is an unknown quantity at this point 
Solved Threads: 0
lwinn213 lwinn213 is offline Offline
Newbie Poster

Re: URGENT: FTP Client / Server using RMI

 
0
  #6
Apr 23rd, 2006
Follow up to previous post. I think I have Sockets issued sorted, I called them before initialising RMI and it seems to have worked.

I have attached a screenshot of the RMI registry and server running, plus the 2 batch files for jwenting's benefit as he said I wasn't running them.

I just need to work out how to run FTP commands using the inetfactory.jar library, i just get connection times out

Any ideas why the connection is not starting, i think it's something to do with the FTP not seeing the server running despite it being set up as a ServerSocket on port 21...hmmm

I will post more code later...

Click here for screenshot as mentioned...

Thanks
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,144
Reputation: jwenting is just really nice jwenting is just really nice jwenting is just really nice jwenting is just really nice 
Solved Threads: 212
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: URGENT: FTP Client / Server using RMI

 
0
  #7
Apr 23rd, 2006
RMI as standard runs on port 1099, not port 21.
As people are clearly allowed to attack me but I'm not allowed to defend myself, I no longer post to this site.
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 8
Reputation: lwinn213 is an unknown quantity at this point 
Solved Threads: 0
lwinn213 lwinn213 is offline Offline
Newbie Poster

Re: URGENT: FTP Client / Server using RMI

 
0
  #8
Apr 23rd, 2006
I know that RMI runs on port 1009, but I need to create an FTP Client Server solution so have decided to use Sockets running on port 21 and the File class, i've scrapped using FTP classes. I will just call the methods which simulate FTP commands using the File class via RMI.

I will get a workaround solution done later.
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,144
Reputation: jwenting is just really nice jwenting is just really nice jwenting is just really nice jwenting is just really nice 
Solved Threads: 212
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: URGENT: FTP Client / Server using RMI

 
0
  #9
Apr 23rd, 2006
You can't have it both ways. Either you use RMI and the ports it gives you, or you code your own sockets implementation and use whatever port you want.
As people are clearly allowed to attack me but I'm not allowed to defend myself, I no longer post to this site.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Java Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC