RMI - at my wits end

C

Chris

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).
 
L

Larry

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
 
E

EJP

Chris said:
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).

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
 
C

Chris

Unfortunately I have to wait for system admin to sort it which could be
days................:-(
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top