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
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
[B]package[/B] server;
[B]import[/B] java.io.*;[B]import[/B] java.lang.*;
[B]import[/B] java.net.*;
[B]import[/B] java.net.InetAddress.*;
[B]import[/B] java.net.UnknownHostException;
[B]import[/B] java.rmi.server.UnicastRemoteObject;
[B]import[/B] java.rmi.RemoteException;
[B]import[/B] java.util.*;
[B]import[/B] com.jscape.inet.ftp.*;
[B]import[/B] server.*;
[B]public[/B] [B]class[/B] RmiFtpImpl [B]extends[/B] UnicastRemoteObject [B]implements[/B] RmiFtp
{ // begin class
[B]private[/B] [B]static[/B] String hostName;
[B]private[/B] String host, user, password;
[B]private[/B] String name;
[B]private[/B] [B]int[/B] port;
String ftpEx;
Login log;
[B]public[/B] RmiFtpImpl(String s) [B]throws[/B] RemoteException
{ // begin rmiftpimpl
[B]super[/B]();
name = s;
} // end rmiftpimpl
[B]public[/B] [B]boolean[/B] doLogin()
[B]throws[/B] 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...");
[B]try[/B] { // begin try
System.out.println ("Attempt to create FTP object...");
InetAddress addr = InetAddress.getLocalHost();
// Get IP Address
[B]byte[/B][] ipaAddr = addr.getAddress();
// Get Host Name
String hostName = addr.getHostName();
host = hostName;
user = "user";
password = "pass";
Ftp ftpconn = [B]new[/B] 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();
[B]return[/B] [B]true[/B];
} // end try
[B]catch[/B] (FtpException ex) { // begin catch
ftpEx = "FTP Exception: " + ex.getMessage();
System.out.println(ftpEx);
[B]return[/B] [B]false[/B];
} // end catch
[B]catch[/B] (UnknownHostException uhe) {
System.out.println("Host Exception Error: " + uhe.getMessage());
[B]return[/B] [B]false[/B];
}
}
}
FTP RMI CLIENT
/*
package client;
/**
*
* @author
*/
[B]import[/B] com.jscape.inet.ftp.*;
[B]import[/B] java.io.*;
[B]import[/B] java.net.*;
[B]import[/B] java.net.MalformedURLException;
[B]import[/B] java.rmi.*;
[B]import[/B] java.rmi.Remote;
[B]import[/B] java.rmi.Naming;
[B]import[/B] java.rmi.RMISecurityManager;
[B]import[/B] java.util.Enumeration;
[B]import[/B] server.RmiFtp;
[B]public[/B] [B]class[/B] RmiFtpClient [B]extends[/B] FtpAdapter [B]implements[/B] FtpListener {
[B]public[/B] [B]static[/B] String servHost;
[B]private[/B] [B]static[/B] String hostName;
[B]public[/B] [B]static[/B] [B]void[/B] main(String[] args) [B]throws[/B] RemoteException {
[B]if[/B] (System.getSecurityManager() == [B]null[/B])
{
System.setSecurityManager([B]new[/B] RMISecurityManager());
}
servHost = "localhost";
[B]if[/B] (args.length < 1) {
System.out.println("Usage: java RmiFtpClient <rmihost>");
}
[B]else[/B] {
servHost = args[0];
}
[B]new[/B] RmiFtpClient();
}
[B]public[/B] RmiFtpClient() {
String host;
String user;
String password;
//int port = 1099;
RmiFtp rmi = [B]null[/B];
[B]try[/B] {
InetAddress addr = InetAddress.getLocalHost();
// Get IP Address
//byte[] ipaAddr = addr.getAddress();
// Get Host Name
String hostName = addr.getHostName();
BufferedReader reader = [B]new[/B] BufferedReader([B]new[/B] 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?");
}
[B]catch[/B](RemoteException re) {
System.out.println("FTP RemoteException Error: " + re.getMessage());
}
[B]catch[/B](IOException io) {
System.out.println("FTP IO Error: " + io.getMessage());
}
[B]catch[/B](NotBoundException e) {
e.printStackTrace();
}
}
}