Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / May 2006

Tip: Looking for answers? Try searching our database.

RMI - at my wits end

Thread view: 
Chris - 15 May 2006 14:15 GMT
Hi all,

I'm new to RMI and I am having real trouble in invoking remote methods.
I have a server program running on my student server which has Java SDK
1.5.0. I seem to have been down every avenue I can think of yet I still
can't sort it.

The jist is that I can connect to the registry ok but not a remote
method. It consistently shows exception:-

java.rmi.ConnectException: Connection refused to host: 138.37.95.141;
nested exc
eption is:
       java.net.ConnectException: Connection timed out: connect
[abridged here]
-------------------

I run the registry using rmiregistry 30280 and I run the Server as
follows:-

java -Djava.security.manager
-Djava.security.policy=file:/home/cb07/RMI/FileServer/java.policy
-Djava.rmi.server.hostname=138.37.95.141
-Djava.rmi.server.codebase=file:/home/cb07/RMI/FileServer/ FileServer

My policy file shows:-
grant
{
      permission java.security.AllPermission;
};

My client code is:-
---------------------------
import java.rmi.*;
import java.io.*;
import javax.swing.*;

public class FileClient{

   public FileClient(){
       try{
           BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
           /*System.out.println("Enter URL");
           String remoteHost = br.readLine();
           int remotePort = 1099; //default value
           try{
               System.out.println("Enter port");
               remotePort = Integer.parseInt(br.readLine());
           }
           catch(NumberFormatException nfe){
               System.out.println(nfe.toString());
           }*/

           String remoteHost = "students.odl.qmul.ac.uk";
           int remotePort = 30280;

           FileServerInt server = (FileServerInt) Naming.lookup("rmi://"+
remoteHost+ ":" + remotePort + "/FileServer");
           System.out.println("Connected to remote FileServer");
           System.out.println("Enter the string you wish the server to
display");
           String serverString = br.readLine();

           server.printString(serverString);

           JFileChooser fc = new JFileChooser();
           fc.setVisible(true);
           System.out.println("Here");

           //return value is what the user presses in the open File
dialog
           int returnVal = fc.showOpenDialog(null);
           System.out.println("Here");

           //if they choose OK
           if (returnVal == JFileChooser.APPROVE_OPTION) {

                 //file now references the selected
                 File file = fc.getSelectedFile();

                 //create a FileInputStream from file location
                 FileInputStream fis = new FileInputStream(file);
                 // Create the byte array to hold the data, the same size
as the file
                 byte [] fileBytes = new byte[(int)file.length()];
                  // Read in the bytes from the file into the byte array
                  int offset = 0;
                  int numRead = 0;
                  while (offset < fileBytes.length &&
                 (numRead=fis.read(fileBytes, offset,
fileBytes.length-offset)) >=
                 0) {
                     offset += numRead;
                 }
                 // Ensure all the bytes have been read in
                 if (offset < fileBytes.length) {
                     throw new IOException("Could not completely read file
"+file.getName());
                 }
                 fis.close();

                 //File is now read in so send the name of and the byte
array
                 //to the remote object
               server.saveFile(file.getName(), fileBytes);
            }
        }
        catch (Exception e) {
            e.printStackTrace();

        }
   }
   public static void main(String args[]) {
       FileClient client = new FileClient();
   }
}

Server Code
-----------------
import java.io.*;
import java.rmi.*;
import java.rmi.server.*;

public class FileServer extends UnicastRemoteObject implements
FileServerInt {

   public FileServer() throws RemoteException{
        super();
   }

   public void saveFile(String name, byte [] bytes) throws
RemoteException{

       System.out.println("Here");
       try{
           File f = new File(name);
           FileOutputStream fs = new FileOutputStream(f);
            fs.write(bytes);
            fs.close();
        }
       catch(IOException e){
           e.printStackTrace();
       }
   }

   public void printString(String str) throws RemoteException{
       System.out.println(str);
   }

   public static void main(String [] args) throws IOException{

       int portNumber = 1099; //default
       BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
       System.out.println("Enter url");
       String host = br.readLine();
       System.out.println("Enter port number");
       try{
           portNumber = Integer.parseInt(br.readLine());
       }
       catch(NumberFormatException nfe){
           System.out.println(nfe.toString());
       }

        try{
           FileServerInt server = new FileServer();
           Naming.rebind("rmi://" + host + ":" + portNumber + "/FileServer",
server);
        }
        catch(Exception e){
           e.printStackTrace();
           System.exit(0);
        }
        System.out.println("Server listening on port number " + portNumber);
    }
}
-------------------------------------------------------------------------------------------------------------------------------------
In both classes the host name and port are students.odl.qmul.ac.uk and
30280.

Does anybody else have any ideas because I'm at the point of throwing
this damn monitor out of the window?

Regards,

Chris

P.S. If I set the hostname property on the server to a single word
instead of an ip address (i.e. -Djava.rmi.server.hostname=ventus) the
connection to the registry is really quick. Idf I use the ip address it
is really slow (about 15 secs to connect to the remote server).
Larry - 15 May 2006 18:58 GMT
Sounds like you are running this on a Windows server?  I'd check the
event log on the server to see if there are error messages pertaining
to RMI.....might give you a clue.

When I was setting up Apache on Windows with PHP, the event log helped
me locate a server startup issue...some files that Apache needed were
encrypted   ;-)

Larry
Chris - 16 May 2006 09:52 GMT
Unfortunately I have to wait for system admin to sort it which could be
days................:-(
EJP - 16 May 2006 02:12 GMT
> Hi all,
>
[quoted text clipped - 188 lines]
> connection to the registry is really quick. Idf I use the ip address it
> is really slow (about 15 secs to connect to the remote server).

RMI or rather InetAddress does a reverse DNS lookup in this situation
and this can be slow. Try setting
-Dsun.net.spi.nameservice.provider.1=dns,sun. See
http://forum.java.sun.com/thread.jspa?forumID=58&threadID=656714


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.