Reputation: 31
I have two RMI java applications:
server
System.setProperty("java.rmi.server.hostname", "0.tcp.ngrok.io");
System.setProperty("java.security.policy", "security.policy");
if (System.getSecurityManager() == null) {
System.setSecurityManager(new SecurityManager());
}
Registry r = LocateRegistry.createRegistry(1099);
r.rebind("Calc", new CalculatorImpl());
client
System.setProperty("java.rmi.server.hostname", "0.tcp.ngrok.io");
System.setProperty("java.security.policy", "security.policy");
Calculator c = (Calculator) Naming.lookup("//0.tcp.ngrok.io:18872"+"/Calc");
int sum = c.add(1, 5);
Communication between the two is done with the help of Ngrok:
ngrok tcp 1099
result
Session Status online
Account nival (Plan: Free)
Version 2.2.8
Region United States (us)
Web Interface http://127.0.0.1:4040
Forwarding tcp://0.tcp.ngrok.io:18872 -> localhost:1099
Connections ttl opn rt1 rt5 p50 p90
5 0 0.00 0.00 5.88 17.03
My problem is :
RemoteException
java.rmi.ConnectException: Connection refused to host: 0.tcp.ngrok.io; nested exception is:
java.net.ConnectException: Connection timed out: connect
I think the problem is at
System.setProperty("java.rmi.server.hostname","0.tcp.ngrok.io");
what should I put on the server and client at "java.rmi.server.hostname"?
(Sorry for my bad english)
Upvotes: 2
Views: 6559
Reputation: 32507
In general it's only required on server side to show on what hostname exported stubs will be available to the world.
It is useful for example, when your server is in the intranet and listens on some interface lets say 192.168.2.1
. NAT proxy can have ports forwarded to that host from internet. Normally RMI registry would report stubs being exported on address 192.168.2.1:someport
. But obviously you cannot hit that address directly java.rmi.server.hostname
comes in action. By setting that property, RMI registry will announce stubs being exported on given hostname
insteed on actual intranet address, allowing clients to get to the stubs from outside world trough forwarded ports on NAT gateway.
So if your server is behind NAT, it should specify the domain that points to your NAT gateway from the Internet. If your server has external IP and its listening on that IP, then this property is not needed.
Upvotes: 4