Search code examples
javarmi

How can a Java RMI client connect to a Java RMI server using a domain name


I am writing an application using Java RMI. I have already created the client and server. However, I am using localhost only. I want to be able to use a domain name for the client to contact the server. I have already registered a domain name: hotel.ddns.net. I wish to be able to keep it inside a personal network, and nothing to do with port forwarding or so. The client and the server are found on different computers. I have been looking for solutions but these are not directly related to my question. Could you guys help me since I am still new to this?

Here is my client code:

package Hotel_;

import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java_rmi.java_rmi_interface;

public class ClientRMI {

    public java_rmi_interface connectToServer() throws NotBoundException{
    try{
        Registry myregistry = LocateRegistry.getRegistry("127.0.0.1",1099);
        java_rmi_interface myinterface = (java_rmi_interface)myregistry.lookup("bookingServer");
        System.out.println("client reaaaaaaady");
        
        
    return myinterface;
      
    }catch(RemoteException e){
        e.printStackTrace();
    }
    
    return null;
    }
}

here is my code for the server

import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.registry.Registry;
import java.rmi.registry.LocateRegistry;

public class ServerRMI {
    public static void main(String[] args){
    try{
        //Timer timer = new Timer();
        //timer.deletefunc();
        Registry myregitry = LocateRegistry.createRegistry(1099);//create a registry that can be 
        accessed on this port
        QueryCentre qs = new QueryCentre();//an instance of QueryCenter where remote methods are 
    found
        Naming.rebind("bookingServer", qs);//naming the server and passing the class where remote 
     methods are found
        System.out.println("server ready and waitiiiinnnggg!!!!!");
    }catch(RemoteException | MalformedURLException e){
        e.printStackTrace();
    }
    }  
}

Solution

  • If you are working entirely within a personal network with private IP addresses (i.e. 192.168.x.x) then DDNS will do nothing for you. It is intended to work with external, routable addresses only.

    You have a few choices, from most complex to least complex they are:

    1. Run a DNS server inside your LAN to map host names to local IP addresses. This is a non-trivial undertaking, but might be interesting if you want to learn more about DNS.
    2. Add lines in /etc/hosts to map names to the local IP addresses. You have to make sure every host that wants to talk to the server has the mapping in its copy of /etc/hosts.
    3. Dispense with hostnames and just use IP addresses.

    I strongly recommend #3 for your simple setup.