| | |
URGENT: FTP Client / Server using RMI
![]() |
•
•
Join Date: Apr 2006
Posts: 8
Reputation:
Solved Threads: 0
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
RMI IMPLEMENTATION
FTP RMI CLIENT
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
Java Syntax (Toggle Plain Text)
package server; import java.net.*; import java.net.UnknownHostException; import java.io.*; import java.util.*; import java.lang.Thread; import java.rmi.*; import java.rmi.Remote; import java.rmi.Naming; import java.rmi.RemoteException; import java.rmi.server.*; import com.jscape.inet.ftp.*; import com.jscape.inet.ftp.FtpException; import server.*; public class RmiFtpServer { //static String hostName = "localhost"; private static String hostName; private static int hostPort = 1099; static RmiFtpImpl rmi; public static void main(String[] args) throws RemoteException, FtpException, UnknownHostException { String host; String port; String badhost; String error; //System.setSecurityManager(new RMISecurityManager()); try { InetAddress addr = InetAddress.getLocalHost(); // Get IP Address byte[] ipaAddr = addr.getAddress(); // Get Host Name String hostName = addr.getHostName(); RmiFtpImpl rmi = new RmiFtpImpl("RmiFtpServer"); System.out.println("RmiFtpServer object created..."); System.out.println("Registering FTP Server with RMI Naming service..."); String serverObjectName = "rmi://localhost/RmiFtpServer"; Naming.rebind(serverObjectName, rmi); System.out.println("RmiFtpServer bound in RMI registry"); host = "Bindings Finished. FTP Server hostname is: " + hostName; System.out.println(host); port = "Waiting for FTP Client requests on port "+ hostPort +" ..."; System.out.println(port); } catch (UnknownHostException uhe) { badhost = "The host computer name you have specified, "+ hostName +" does not match your real computer name."; System.out.println(badhost); //System.exit(1); } catch (RemoteException re) { error = "Error starting service :"+re; System.out.println(error); //System.exit(1); } catch (MalformedURLException url) { System.out.println("RmiFtpServer URL Error: " + url.getMessage()); } } }
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(); } } }
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.
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.
•
•
Join Date: Apr 2006
Posts: 8
Reputation:
Solved Threads: 0
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
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
Java Syntax (Toggle Plain Text)
package server;
Java Syntax (Toggle Plain Text)
import java.net.*; import java.net.UnknownHostException; import java.io.*; import java.util.*; import java.lang.Thread; import java.rmi.*; import java.rmi.Remote; import java.rmi.Naming; import java.rmi.RemoteException; import java.rmi.server.*; import com.jscape.inet.ftp.*; import server.*; public class RmiFtpServer extends Thread { //static String hostName = ; // The port which FTP server will connect to public static final int SERVER_PORT = 21; // The data port public static final int SERVER_DATA_PORT = 20; // The server name public static final String SERVER_HOST = "localhost"; // The port this server is listening on private int portListen; // private static int hostPort = 1099; private static RmiFtpImpl rmi; private static String hostName; private static String r; private Socket incoming; private Socket outgoing; private int counter; public static void main(String[] args) throws RemoteException, UnknownHostException { String host; String port; String badhost; String error; //Socket outgoing; //BufferedReader in = null; //PrintWriter out = null; String line; int i = 1; try { InetAddress addr = InetAddress.getLocalHost(); // Get IP Address byte[] ipAddr = addr.getAddress(); // Get hostname String hostname = addr.getHostName(); System.out.println("begin here..."); RmiFtpImpl rmi = new RmiFtpImpl("RmiFtpServer"); System.out.println("RmiFtpServer object created..."); System.out.println("Registering FTP Server with RMI Naming service..."); String serverObjectName = "rmi://localhost/RmiFtpServer"; Naming.rebind(serverObjectName, rmi); hostName = SERVER_HOST; System.out.println("RmiFtpServer bound in RMI registry"); host = "Bindings Finished. FTP Server hostname is : " + hostName; System.out.println(host); ServerSocket s = new ServerSocket(21); int portListen = SERVER_PORT; port = "Waiting for FTP Client requests on port "+ portListen + " ..."; System.out.println(port); System.out.println("begin socket..."); Socket outgoing = new Socket(hostName, 20); //String request="login\r\n"; byte[] b = new byte[1024]; // OutputStream out = outgoing.getOutputStream(); OutputStream out = outgoing.getOutputStream(); BufferedInputStream in = new BufferedInputStream(outgoing.getInputStream()); System.out.println(in.read(b)); //System.out.println("start client data transfer..."); //String clientData = in.readLine(); //System.out.println("receive data from client " + clientData); //System.out.println("send data to client " + clientData); //out.println("This is message from server"); //out.flush(); //out.write(request.getBytes()); //System.out.println(in.read(b)); //for(;;) //{ Socket incoming = s.accept(); System.out.println(s.getInetAddress()); new RmiFtpServer(incoming,i).start(); i++; // } } catch (UnknownHostException uhe) { badhost = "The host computer name you have specified, "+ hostName +" does not match your real computer name."; System.out.println(badhost); //System.exit(1); } catch (RemoteException re) { error = "Error starting service :"+re; System.out.println(error); //System.exit(1); } catch (MalformedURLException url) { System.out.println("RmiFtpServer URL Error: " + url.getMessage()); } catch (IOException io) { System.out.println("RmiFtpServer IO Error: " + io.getMessage()); } } public RmiFtpServer(Socket income, int c) { System.out.println("Entered Constructor"); incoming = income; counter = c; } public void run() { int lng, lng1, lng2, i, ip1, ip2, ip = 1, h1; String a1, a2, di, str1, user="", host, dir; System.out.println(r); dir = r; InetAddress inet; InetAddress localip; try { inet = incoming.getInetAddress(); localip = inet.getLocalHost(); host = inet.toString(); h1 = host.indexOf("/qc308_serv"); host = host.substring(h1 + 1); System.out.println(host); BufferedReader in = new BufferedReader (new InputStreamReader(incoming.getInputStream())); PrintWriter out = new PrintWriter(incoming.getOutputStream(),true); out.println("SERVER: 220 FTP Server ready.\r"); } catch (Exception e) { System.out.println(e); } }
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.
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.
•
•
Join Date: Apr 2006
Posts: 8
Reputation:
Solved Threads: 0
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
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
•
•
Join Date: Apr 2006
Posts: 8
Reputation:
Solved Threads: 0
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.
I will get a workaround solution done later.
![]() |
Similar Threads
Other Threads in the Java Forum
- Previous Thread: About Me Box
- Next Thread: how to set tab size
| Thread Tools | Search this Thread |
-xlint add android api applet application applications array arrays automation bank bi binary blackberry bluetooth chat class clear client code compile compiler component database development dice digit eclipse equation error event formatingtextintooltipjava fractal freeze functiontesting game gameprogramming givemetehcodez graphics gui health html hyper ide idea image infinite int integer j2me java javame javaprojects jetbrains jni jpanel jtable julia learningresources linux list main map method methods mobile myregfun netbeans nonstatic notdisplaying openjavafx pearl problem program project qt recursion repositories scanner screen scrollbar server set sms sort sorting spamblocker sql sqlserver storm string superclass swing system thread threads tree variablebinding windows xor






