我有两个RMI java应用程序:
服务器
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() );
客户端
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);
两人之间的沟通是在Ngrok的帮助下完成的:
ngrok tcp 1099
结果
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
我的问题是:
RemoteException
java.rmi.ConnectException: Connection refused to host: 0.tcp.ngrok.io; nested exception is:
java.net.ConnectException: Connection timed out: connect
我认为问题出在
System.setProperty("java.rmi.server.hostname","0.tcp.ngrok.io");
我应该在" java.rmi.server.hostname" 上把它放在服务器和客户端上?
(抱歉我的英语不好)
答案 0 :(得分:0)
一般来说,服务器端只需要显示哪些主机名导出的存根可供全世界使用。
例如,当您的服务器位于Intranet中并侦听某些界面时,可以说192.168.2.1
。 NAT代理可以将端口从Internet转发到该主机。通常,RMI注册表将报告在地址192.168.2.1:someport
上导出的存根。但显然你无法直接点击该地址java.rmi.server.hostname
。通过设置该属性,RMI注册表将在实际内部网地址上的给定hostname
上导出导出存根,允许客户端通过NAT网关上的转发端口从外部世界获取存根。
因此,如果您的服务器位于NAT之后,则应指定从Internet指向NAT网关的域。如果您的服务器具有外部IP并且侦听该IP,则不需要此属性。