Skip to Main Content

Java Programming

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!

Problems with java applet accessed via SSH local tunnel -Connection refused

807580Sep 25 2009
I have a java application (web-based applet interface) that I support that works over a direct Internet connection, however if I try to reach the interface over an SSH tunnel, the applet loads but the server data exchange does not take place. I created a simple java application (based upon a Java RMI tutorial) to prove that it's not my application, with the same lack of success.

Java source files from Sun RMI tutorial.

Hello.java
package examples.hello;

import java.rmi.Remote;
import java.rmi.RemoteException;

public interface Hello extends Remote {
    String sayHello() throws RemoteException;
}
HelloApplet.java
package examples.hello;

import java.applet.Applet;
import java.awt.Graphics;
import java.rmi.Naming;
import java.rmi.RemoteException;

public class HelloApplet extends Applet {

    String message = "blank";

    // "obj" is the identifier that we'll use to refer
    // to the remote object that implements the "Hello"
    // interface
    Hello obj = null;

    public void init() {
        try {
            obj = (Hello)Naming.lookup("//" +
                         getCodeBase().getHost() + "/HelloServer");
            message = obj.sayHello();
        } catch (Exception e) {
            System.out.println("HelloApplet exception: " +
                                    e.getMessage());
            e.printStackTrace();
        }
    }

    public void paint(Graphics g) {
        g.drawString(message, 25, 50);
    }
}
HelloImpl.java
package examples.hello;

import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.RMISecurityManager;
import java.rmi.server.UnicastRemoteObject;

public class HelloImpl extends UnicastRemoteObject
    implements Hello {

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

    public String sayHello() {
        return  "Hello World!";
    }

    public static void main(String args[]) {

        // Create and install a security manager
        if (System.getSecurityManager() == null) {
            System.setSecurityManager(new RMISecurityManager());
        }
        try {
            HelloImpl obj = new HelloImpl();
            // Bind this object instance to the name "HelloServer"
            Naming.rebind("HelloServer", obj);
            System.out.println("HelloServer bound in registry");
        } catch (Exception e) {
            System.out.println("HelloImpl err: " + e.getMessage());
            e.printStackTrace();
        }
    }
}
Here's the URL I use to reach the application using a direct connection that works:

http://my_remote_server/~my_login/hello.html

Here's the java console output from the successful connection.

basic: Added progress listener: sun.plugin.util.GrayBoxPainter$GrayBoxProgressListener@21d23b
basic: Applet loaded.
basic: Starting applet teardown
basic: Applet resized and added to parent container
basic: PERF: AppletExecutionRunnable - applet.init() BEGIN ; jvmLaunch dt 189383 us, pluginInit dt 5964268764 us, TotalTime: 5964458147 us
basic: Finished applet teardown
network: Connecting http://my_remote_server:1099/ with proxy=DIRECT
network: Connecting http://my_remote_server:64954/ with proxy=DIRECT
basic: Applet initialized
basic: Removed progress listener: sun.plugin.util.GrayBoxPainter$GrayBoxProgressListener@21d23b
basic: Applet made visible
basic: Starting applet
basic: completed perf rollup
basic: Applet started
basic: Told clients applet is started


Now using a local SSH tunnel from a PC to the remote server with the following URL doesn't work:

http://localhost:6010/~my_login/hello.html

Here's the Java console output from the failing connection:

basic: Added progress listener: sun.plugin.util.GrayBoxPainter$GrayBoxProgressListener@c3e9e9
basic: Applet loaded.
basic: Applet resized and added to parent container
basic: PERF: AppletExecutionRunnable - applet.init() BEGIN ; jvmLaunch dt 189383 us, pluginInit dt 5216270552 us, TotalTime: 5216459935 us
basic: Starting applet teardown
network: Connecting http://localhost:1099/ with proxy=DIRECT
basic: Finished applet teardown
basic: Loading Java Applet ...
HelloApplet exception: Connection refused to host: localhost; nested exception is:
java.net.ConnectException: Connection refused: connect
java.rmi.ConnectException: Connection refused to host: localhost; nested exception is:
java.net.ConnectException: Connection refused: connect
at sun.rmi.transport.tcp.TCPEndpoint.newSocket(Unknown Source)
at sun.rmi.transport.tcp.TCPChannel.createConnection(Unknown Source)
at sun.rmi.transport.tcp.TCPChannel.newConnection(Unknown Source)
at sun.rmi.server.UnicastRef.newCall(Unknown Source)
at sun.rmi.registry.RegistryImpl_Stub.lookup(Unknown Source)
at java.rmi.Naming.lookup(Unknown Source)
at examples.hello.HelloApplet.init(HelloApplet.java:57)
at sun.plugin2.applet.Plugin2Manager$AppletExecutionRunnable.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: java.net.ConnectException: Connection refused: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(Unknown Source)
at java.net.PlainSocketImpl.connectToAddress(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at sun.rmi.transport.proxy.RMIDirectSocketFactory.createSocket(Unknown Source)
at sun.rmi.transport.proxy.RMIMasterSocketFactory.createSocket(Unknown Source)
... 9 more
basic: Applet initialized
basic: Removed progress listener: sun.plugin.util.GrayBoxPainter$GrayBoxProgressListener@c3e9e9
basic: Applet made visible
basic: Starting applet
basic: completed perf rollup
basic: Applet started
basic: Told clients applet is started

Here is the java command I used to start the server on the remote system:

/opt/java1.5/bin/IA64N/java -Djava.rmi.server.hostname=my_remote_server -Djava.rmi.server.codebase=http://my_remote_server/~my_login/myclasses/ -Djava.security.policy
=/home/my_login/getStart/policy examples.hello.HelloImpl

I have done some research on this issue and everyone seems to point towards the use of the "java.rmi.server.hostname" or "java.rmi.server.useLocalHostname" properties, which after using made no difference in the problem. Clearly, the value of localhost that is passed in the URL is the cause of the problem. The remote server doesn't have the ability to parse localhost into the correct address.

Is it even possible to access a java applet in this fashion over an SSH tunnel? If so, what am I missing in the code? How do I accomplish this task?
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Oct 23 2009
Added on Sep 25 2009
0 comments
278 views