Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

EasyRMI: Connection Refused to localhost

62 views
Skip to first unread message

Rob

unread,
Jul 10, 2002, 4:33:58 PM7/10/02
to
I've read nearly every newsgroup article on
"+java.rmi.ConnectException +127.0.0.1",
checked out
"http://java.sun.com/docs/books/tutorial/rmi/index.html",
studied
"http://java.sun.com/products/jdk/rmi/index.html",
and still I can't figure this out. Any pointers you can give are
GREATLY appreciated.

-rob


HERE IT IS...
Like many others, my RMI Client-Server application runs fine when
running on the same host but I am getting a

java.rmi.ConnectException: Connection refused to host: 127.0.0.1;

when I try to run the client application from a remote host. This
happens even when I specify the remote server IP address as the rmi
URL. From what I've read, the localhost connection may be an attempt
to download Stub classes, but the use of the codebase setting should
keep this from happening, right? So WHY OH WHY is it still trying to
connect to localhost, and for what???

WHAT THE PROGRAM DOES:
+ The client takes 2 arguments, "server" and "message", and sends
"message" to the specified "server" via RMI.
+ The server simply prints the "message" to stdout.

KEY POINTS OF CODING AND CONFIGURATION:
+ I'm running the client as an application, not an applet.
+ I'm using a "file:///" URL for the codebase setting (server &
client)
-Djava.rmi.server.codebase=file:///tmp/EasyRMI.jar
+ I'm using wide-open java.policy files on both client and server
grant
{
permission java.net.SocketPermission "*",
"listen,connect,accept,resolve";
permission java.io.FilePermission "/tmp/EasyRMI.jar", "read";
permission java.security.AllPermission;
}
+ I'm using J2SE version 1.4 on Linux (RH73)
java version "1.4.0_01"
Java(TM) 2 Runtime Environment, Standard Edition (build
1.4.0_01-b03)
Java HotSpot(TM) Client VM (build 1.4.0_01-b03, mixed mode)

COMMANDLINE CONFIGURATION AND RUNTIME OUTPUT:
SERVER:
java -Djava.rmi.server.codebase=file:///tmp/EasyRMI.jar -classpath
EasyRMI.jar -Djava.security.policy=./java.policy MsgPrinter

SERVER OUTPUT:
Starting MsgPrinter server
Binding Server
Server is waiting...

CLIENT (RUNNING ON SERVER HOST):
java -classpath EasyRMI.jar -Djava.security.policy=./java.policy
MsgSender localhost "This works just fine"

CLIENT OUTPUT:
Starting MsgSender client...
File Server lookup: url = rmi://localhost/PrintMsg
Sending message >This works just fine<

SERVER OUTPUT:
String sent from the other side: >This works just fine<

CLIENT (RUNNING ON REMOTE HOST):
java -classpath EasyRMI.jar -Djava.security.policy=./java.policy
MsgSender 192.168.100.30 "This should work too"

CLIENT OUTPUT:
Starting MsgSender client...
File Server lookup: url = rmi://192.168.100.30/PrintMsg
Sending message >This should work too<
An error occurred
java.rmi.ConnectException: Connection refused to host: 127.0.0.1;
nested exception is:
java.net.ConnectException: Connection refused
at sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:567)
at sun.rmi.transport.tcp.TCPChannel.createConnection(TCPChannel.java:185)
at sun.rmi.transport.tcp.TCPChannel.newConnection(TCPChannel.java:171)
at sun.rmi.server.UnicastRef.invoke(UnicastRef.java:101)
at PrintMsgImp_Stub.PrintMsg(Unknown Source)
at MsgSender.main(MsgSender.java:42)
Caused by: java.net.ConnectException: Connection refused
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:295)
at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:161)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:148)
at java.net.Socket.connect(Socket.java:425)
at java.net.Socket.connect(Socket.java:375)
at java.net.Socket.<init>(Socket.java:290)
at java.net.Socket.<init>(Socket.java:118)
at sun.rmi.transport.proxy.RMIDirectSocketFactory.createSocket(RMIDirectSocketFactory.java:22)
at sun.rmi.transport.proxy.RMIMasterSocketFactory.createSocket(RMIMasterSocketFactory.java:122)
at sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:562)
... 5 more
Connection refused to host: 127.0.0.1; nested exception is:
java.net.ConnectException: Connection refused


THE CODE:

MsgSender.java
-----------------------------------------------------------------------
import java.io.*;
import java.util.*;
import java.rmi.*;
import java.rmi.RMISecurityManager;


public class MsgSender
{
public static void main(String[] args) throws IOException
{
int argc = args.length;
PrintMsg ps_obj = null;

if ( 2 != argc )
{
System.out.println("Illegal Arguments");
System.out.println("\tArg 1: Server,\n\tArg 2: String to
Print");
System.exit(0);
}
String server = args[0];
String StringToPrint = args[1];

System.setSecurityManager( new RMISecurityManager() );

try
{
System.out.println("Starting MsgSender client...");

String url = new String ("rmi://" + server + "/PrintMsg");
/* Tried this too
* String url = new String ("//" + server + "/PrintMsg");
*/

System.out.println("File Server lookup: url = " + url);

ps_obj = (PrintMsg)Naming.lookup( url );

if ( ps_obj != null )
{
System.out.println("Sending message >" + StringToPrint + "<");
ps_obj.PrintMsg(StringToPrint);
}
else
{
System.out.println("Requested object is null.");
}
}
catch (Exception e)
{
System.out.println("An error occurred");
e.printStackTrace();
System.out.println(e.getMessage());
}
}
}
=======================================================================

MsgPrinter.java
-----------------------------------------------------------------------
import java.util.*;
import java.rmi.*;
import java.rmi.RMISecurityManager;

public class MsgPrinter
{
public static void main( String args[] )
{
System.setSecurityManager( new RMISecurityManager() );

try
{
System.out.println("Starting MsgPrinter server");
PrintMsgImp ps_obj = new PrintMsgImp();

System.out.println("Binding Server");
Naming.rebind("PrintMsg", ps_obj);
System.out.println("Server is waiting...");
}
catch (Exception e)
{
System.out.println("An error occurred");
e.printStackTrace();
System.out.println(e.getMessage());
}
}
}
=======================================================================

PrintMsgImp.java
-----------------------------------------------------------------------
import java.io.*;
import java.rmi.*;
import java.rmi.server.*;

class PrintMsgImp extends UnicastRemoteObject implements PrintMsg
{
PrintMsgImp() throws java.rmi.RemoteException
{
}

public void PrintMsg(String daString) throws
IOException, java.rmi.RemoteException
{
System.out.println( "String sent from the other side:
>"+daString+"<");
return;
}
}
=======================================================================

PrintMsg.java
-----------------------------------------------------------------------
import java.io.*;
import java.rmi.*;

public interface PrintMsg extends java.rmi.Remote
{
public void PrintMsg(String daString)
throws java.rmi.RemoteException, IOException;
}

=======================================================================

Makefile
-----------------------------------------------------------------------
CLSPATH=.

all: MsgSender.class MsgPrinter.class PrintMsgImp_Stub.class
EasyRMI.jar

MsgSender.class: MsgSender.java
javac -classpath $(CLSPATH) MsgSender.java

MsgPrinter.class: MsgPrinter.java
javac -classpath $(CLSPATH) MsgPrinter.java

PrintMsgImp_Stub.class: PrintMsgImp.java
rmic -classpath $(CLSPATH) PrintMsgImp


EasyRMI.jar: MsgSender.class MsgPrinter.class PrintMsg.class
jar -cf EasyRMI.jar MsgSender.class MsgPrinter.class \
PrintMsg.class PrintMsgImp.class PrintMsgImp_Stub.class \
PrintMsgImp_Skel.class

clean:
rm -f *.class
rm -f EasyRMI.jar
=======================================================================

Esmond Pitt

unread,
Jul 10, 2002, 9:40:56 PM7/10/02
to
The problem here is that the server thinks its IP address is 127.0.0.1
and has put that into the stub when exporting. It is, but there's a
better one. Try setting java.rmi.server.hostname to the public IP
address at the server. Not sure why this is happening: is there anything
odd about the IP configuration of the host the server is running in?
e.g. does it lack both a DNS entry and a 'hosts' file?

You could also try Naming.rebind("rmi://localhost/PrintMsg") in the
server instead of what you have: it's more correct, but I don't see why
this would make a difference to this issue.

EJP

Rob

unread,
Jul 11, 2002, 6:18:26 AM7/11/02
to
Esmond Pitt <esmon...@not.bigpond.com> wrote in message news:<3D2CE28B...@not.bigpond.com>...

> The problem here is that the server thinks its IP address is 127.0.0.1
> and has put that into the stub when exporting. It is, but there's a
> better one. Try setting java.rmi.server.hostname to the public IP
> address at the server. Not sure why this is happening: is there anything
> odd about the IP configuration of the host the server is running in?
> e.g. does it lack both a DNS entry and a 'hosts' file?
>
> You could also try Naming.rebind("rmi://localhost/PrintMsg") in the
> server instead of what you have: it's more correct, but I don't see why
> this would make a difference to this issue.
>
> EJP
>

THANK YOU!!! :-) Manually setting java.rmi.server.hostname worked!
(woo hoo)

BTW: I'm using my laptop as the development 'server'. Since it's not
parked at a particular IP address, it's /etc/hosts file does not
reflect the IP that java wants to export (192.168.100.30). The only
reference to itself is the loopback address, and there is no DNS
server reference.

I imagine I will have similar problems if I want to do some funky
port-forwarding acrobatics because of the java.rmi.activation.port
property. IOW, if different clients use different ports to get to the
same server... that server is only going to want to embed one (1)
explicit port in the exported Stub. Is there any way to force the
client into making the host:port decision rather than relying on the
server-provided settings?

Esmond Pitt

unread,
Jul 11, 2002, 10:13:32 PM7/11/02
to

Rob wrote:
>
> Is there any way to force the
> client into making the host:port decision rather than relying on the
> server-provided settings?

No.

0 new messages