Skip to Main Content

Java APIs

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

RMI call fails if the server runs on my laptop and the client elsewhere

843793Dec 5 2002 — edited Dec 6 2002
I'm experiencing a strange problem:
I am testing an RMI Chat client/server app.
The app was working perfectly well until I tested running the server on my laptop.
The results are those:
the server seems to be running well but when a client on an other machine tries to call any method from it,
the calls fails with an RemoteException/ConnectException/"Connection refused to host: 127.0.0.1".
The client can talk to the rmi registry created by the server and list the services but it looks like the lookup returns a stub which indicates 127.0.0.1 as the remote address.

Again, the client/server works fine on any other host of the LAN and they work fine if running both on the laptop.

All the test computers are running Debian GNU/linux with jdk1.4.0_01.

Here are interresting parts of the code:

//------------------------------- Chat Client -------------------------
package examples.chat;
import java.rmi.*;
import java.rmi.server.*;
import java.rmi.registry.*;
import java.net.MalformedURLException ;
import java.io.*;
public class ChatImpl2
//extends UnicastRemoteObject
implements Chat,Serializable

{
private ChatServer cs; //reference to Chat method server
private ServerTalker st; //Thread for handling message sending
private String name;
private String serverURI;
public ChatImpl2(String n, String s) throws RemoteException
{
super();
System.err.println("Starting up Chatter.");
name= n;
serverURI="//"+s+"/ChatServerImpl";
}
public void inLoop(){
String msg=new String();
BufferedReader inBR=new BufferedReader(new InputStreamReader(System.in));
//if (System.getSecurityManager() == null) {
// System.setSecurityManager(new RMISecurityManager());
//}
try {
//export our remote methods
UnicastRemoteObject.exportObject(this);
//lookup the server's remote object in remote registry
cs = (ChatServer) Naming.lookup(serverURI);
System.err.println("Lookup done\nServices in registry:");
String strlst[]=Naming.list(serverURI);
for (int i=0;i<strlst.length;i++){
System.err.println(" "+strlst);
}
cs.listChatters();
System.err.println("List Chatters call done");
cs.register(this, name);
//start a communication thread
st = new ServerTalker(cs, name);
} catch (RemoteException e) {
System.err.println("Couldn't locate registry ( "+serverURI+" ).\n"+e);
System.exit(0);

} catch (MalformedURLException e) {
System.err.println("Bad binding URL: " + e);
System.exit(0);
} catch (NotBoundException e) {
System.err.println("Service not bound.");
System.exit(0);
}
while (true) {
try{
//System.err.print("\033[32;1mmesg:\033[0m");
msg=inBR.readLine();
if(msg==null || msg.equals("/quit"))
return;
if (!st.addMessage(new Message(name, msg))) //failed?
System.err.println("\033[32;1m*** Server Error\033[0m");

} catch (IOException e){
System.err.println("\nIOException\n");
}
}
}
public synchronized void chatNotify(Message m) throws RemoteException
{
System.out.println("\033[31;1m"+m.getSender()+"\033[0m"+ ": " + m.getMessage() + "\n");
}
public static void main(String[] args){
ChatImpl2 c;
String s;
if (args.length==0){
System.err.println("Usage: Chat SeverDNSName [nick]");
System.exit(0);
}
if (args.length==1)
s="test";
else
s=args[1];


try {
c=new ChatImpl2(s,args[0]);
c.inLoop();
} catch (RemoteException e) {
System.err.println("\nRemoteException\n");
}
System.exit(0);
}
}

// -------------------------------------------------------------------
//---------------------------- Chat Server ----------------------------
package examples.chat;
import java.rmi.*;
import java.rmi.server.*;
import java.rmi.registry.*;
import java.util.*;

public class ChatServerImpl
extends UnicastRemoteObject
implements ChatServer
{
private Vector chatters = new Vector();

public ChatServerImpl() throws RemoteException
{
System.out.println("\033[32;1mInitializing Server."+"\033[0m");
}
public static void main(String args[])
{
Registry reg;

//Set security manager to allow stub loading over the network
System.setSecurityManager(new RMISecurityManager());
try {
ChatServerImpl cs = new ChatServerImpl();

//create registry running on port 5050
reg = LocateRegistry.createRegistry(5050); // CREATE REGISTRY
//bind cs in registry
reg.bind("ChatServerImpl", cs);
System.out.println("\033[32;1mServer Ready.\033[0m");
} catch (AlreadyBoundException e) {
System.out.println("\033[32;1mName is already bound: " + e+"\033[0m");
System.exit(0);
} catch (RemoteException e) {
System.out.println("\033[32;1mGeneral Server Error: " + e+"\033[0m");
System.exit(0);
}
}
synchronized public void register(Chat c, String name)
{
chatters.addElement(new Talker(c, name));
//System.err.println("coucou");
}
synchronized public void unregister(String name)
{
Talker c;
for (int i = 0; i < chatters.size(); i++)
{
c = (Talker) chatters.elementAt(i);
if (name.equals(c.getChatterName()))
{
chatters.removeElementAt(i);
return;
}
}
}
public String[] listChatters()
{
String list[] = new String[chatters.size()];
Talker c;

for (int i = 0; i < list.length; i++)
{
c = (Talker) chatters.elementAt(i);
list[i] = c.getChatterName();
}
return list;
}
synchronized public void postMessage(Message m)
{
Talker t;

for (int i = 0; i < chatters.size(); i++)
{
t = (Talker) chatters.elementAt(i);
if (!t.addMessage(m))
//remove Talker, if add failed
chatters.removeElementAt(i);
}
}
}
//------------------------------------------------------------------------

Here is a log of the tests:
Successful test between siudmak and renoir:
siudmak!khd [13] pwd
/pirates/staff1/khd/mycvs/tramdos/src/rmichat
siudmak!khd [14] ./server.sh
Initializing Server.
Server Ready.

dooms@renoir /usr/staff/dooms/boulot/tramdos/src/rmichat [46]$ ./client.sh siudmak:5050 greg
Starting up Chatter.
Lookup done
Services in registry:
rmi://siudmak:5050/ChatServerImpl
List Chatters call done
SYSTEM: Connected greg

test
greg: test

-----------------------------------------
failing test: client still on renoir, server on the laptop (yhwh and dhcp6):

dtr@yhwh /home/dtr/boulot/tramdos/cvs/tramdos/src/rmichat [58]$ ./server.sh
Initializing Server.
Server Ready.

dooms@renoir /usr/staff/dooms/boulot/tramdos/src/rmichat [47]$ ./client.sh dhcp6:5050 greg
Starting up Chatter.
Lookup done
Services in registry:
rmi://dhcp6:5050/ChatServerImpl
Couldn't locate registry ( //dhcp6:5050/ChatServerImpl ).
java.rmi.ConnectException: Connection refused to host: 127.0.0.1; nested exception is:
java.net.ConnectException: Connection refused

-----------------------------------------------------------------
Here are the bash scripts:
client.sh:
#!/bin/bash
java -Djava.security.policy=policy examples.chat.ChatImpl2 $1 $2

server.sh:
#!/bin/bash
java -Djava.security.policy=policy examples.chat.ChatServerImpl

policy:
grant {
permission java.security.AllPermissions;
permission java.net.SocketPermission "*", "accept,resolve";
permission java.net.SocketPermission "*", "connect,resolve";
};
------------------------------------------------------------------------

Thank you in advance for your help.
Gr�goire Dooms
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jan 3 2003
Added on Dec 5 2002
4 comments
283 views