AkshayShrivas
AkshayShrivas

Reputation: 21

Port specific RMI application

Question : How do I make my RMI application to be IP and Port specific ? It means I want that when my application initializes then it'll ask for port and root systems IP address and a port where application could execute independently

Upvotes: 1

Views: 119

Answers (1)

user207421
user207421

Reputation: 311050

Apart from the part where you ask the user for the data, in an RMI client this is just a matter of building the lookup string for Naming.lookup() dynamically instead of statically: e.g. instead of

MyRemote mr = (MyRemote)Naming.lookup("rmi://somehost:someport/MyRemote")

you would have

String host; // initialized by dialogue with user
int port;    // initialized by dialogue with user
MyRemote mr = (MyRemote)Naming.lookup("rmi://"+host+":"+port+"/MyRemote")

At the RMI server end, the port number is provided to super() when constructing your remote object, if you extend UnicastRemoteObject, otherwise to UnicastRemoteObject.exportObject() if you don't extend UnicastRemoteObject. Normally you don't need to specify your own host at all; an exception is if you are behind a NAT device or some other form of port-forwarding firewall, in which case you need to specify the host in the java.rmi.server.hostname property in the server JVM, before you export any remote objects (including the Registry).

Upvotes: 1

Related Questions