Java RMI cannot connect to host from external client

I used RMI in this project for a while. I received a client program for connecting (among other things) to the server when it was launched over the local network, however, when starting over the Internet, I encountered the following exception:

java.rmi.ConnectException: Connection refused to host: (private IP of host machine); nested exception is: 
java.net.ConnectException: Connection timed out: 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.invoke(Unknown Source)
at java.rmi.server.RemoteObjectInvocationHandler.invokeRemoteMethod(Unknown Source)
at java.rmi.server.RemoteObjectInvocationHandler.invoke(Unknown Source)
at $Proxy1.ping(Unknown Source)
at client.Launcher$PingLabel.runPing(Launcher.java:366)
at client.Launcher$PingLabel.<init>(Launcher.java:353)
at client.Launcher.setupContentPane(Launcher.java:112)
at client.Launcher.<init>(Launcher.java:99)
at client.Launcher.main(Launcher.java:59)

Caused by: java.net.ConnectException: Connection timed out: 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)
... 12 more

This error is reminiscent of an early implementation of RMI, and I can get an error assembly if I run the client locally without starting the server program. For me, Connection Timed Out means a problem with the server response.

Here is the client initiation:

public static void main(String[] args)
{
    try
    {
        String host = "<WAN IP>";
        Registry registry = LocateRegistry.getRegistry(host, 1099);
        Login lstub = (Login) registry.lookup("Login Server");
        Information istub = (Information) registry.lookup("Game Server");
        new Launcher(istub, lstub);

    }
    catch (RemoteException e)
    {
        System.err.println("Client exception: " + e.toString());
        e.printStackTrace();
    }
    catch (NotBoundException e)
    {
        System.err.println("Client exception: " + e.toString());
        e.printStackTrace();
    }
}

Interestingly, Remote Exception is not thrown here. Here is the server initiation:

public static void main(String args[])
{
    try
    {
        GameServer gobj = new GameServer();
        Information gstub = (Information) UnicastRemoteObject.exportObject(
                gobj, 1099);
        Registry registry = LocateRegistry.createRegistry(1099);
        registry.bind("Game Server", gstub);

        LoginServer lobj = new LoginServer(gobj);
        Login lstub = (Login) UnicastRemoteObject.exportObject(lobj, 7099);

        // Bind the remote object stub in the registry
        registry.bind("Login Server", lstub);

        System.out.println("Server ready");
    }
    catch (Exception e)
    {
        System.err.println("Server exception: " + e.toString());
        e.printStackTrace();
    }
}

Bad practice with a trick (Exception e) I know, but carrying with me.

, , , WAN, , :

private class PingLabel extends JLabel
{
    private static final long serialVersionUID = 1L;

    public PingLabel()
    {
        super("");
        runPing();
    }

    public void setText(String text)
    {
        super.setText("Ping: " + text + "ms");
    }

    public void runPing()
    {
        try
        {
            PingThread pt = new PingThread();
            gameServer.ping();
            pt.setRecieved(true);
            setText("" + pt.getTime());
        }
        catch (RemoteException e)
        {
            e.printStackTrace();
        }
    }
}

, ping. ping(), gameserver , in .

, 1099 7099 ( ).

- , /- ? , .

EDIT: , . :

Client exception: java.rmi.ConnectException: Connection refused to host: (-WAN IP NOT LOCAL IP-);

, , .

, ( . , , ( ). , .

+3
1

NAT ( )? , (, 10.x 192.169.x ..).

, IP- ,

-Djava.rmi.server.hostname=host_name_or_public_ip
+2

Source: https://habr.com/ru/post/1750096/


All Articles