我正在尝试使用一个简单的程序在 Java RMI 上进行回调,并且它在 localhost 和 LAN 中运行良好,但是在 NAT 路由器后面执行此操作时遇到问题。
如果我的服务器实现,我会被拒绝连接extends UnicastRemoteObject
。如果我不这样做extends UnicastRemoteObject
并手动导出对象UnicastRemoteObject.exportObject(services, 0);
(
我像这样在注册表中绑定服务:
public class Server {
private static final String SRVHOST = "<<external ip>>";
private static Registry registry;
public static void main(String[] args) {
System.setProperty("java.rmi.server.hostname", SRVHOST);
System.setProperty("java.rmi.server.useCodebaseOnly","false");
System.setProperty("java.security.policy","src/srv.policy");
if(System.getSecurityManager()==null)
System.setSecurityManager(new RMISecurityManager());
registry = LocateRegistry.createRegistry(Registry.REGISTRY_PORT);
ServerInterface services = new ServerImpl();
registry.rebind("ServerAPI", services);
}
}
客户获得服务:
public class Client {
private static final String SRVHOST = "localhost";
private static Registry registry;
private static ClientInterface callback;
public static void main(String[] args) {
System.setProperty("java.security.policy", "src/cli.policy");
if(System.getSecurityManager()==null)
System.setSecurityManager(new RMISecurityManager());
registry = LocateRegistry.getRegistry(SRVHOST, Registry.REGISTRY_PORT);
ServerInterface services = (ServerInterface) registry.lookup("ServerAPI");
callback = new ClientImpl();
int nCallback = services.setClientInterface(callback);
...
<<do_things>>
}
}
该方法setClientInterface(...)
获取客户端对象并将其存储在ArrayList
.
我不知道有什么问题。有人能帮我吗?
所有代码都在这里发布:
政策文件:
srv.policy - http://pastebin.com/LTsKSG3r
cli.policy - http://pastebin.com/zqxaRyT3
包:com.rmcallback.client
Client.java - http://pastebin.com/Xus04JZK
ClientImpl.java - http://pastebin.com/SdQXMDzs
ClientInterface.java - http://pastebin.com/S9PxT5vC
包:com.rmcallback.server
Server.java - http://pastebin.com/cxQcWy1Q
ServerImpl.java - http://pastebin.com/nqdGPbPr
ServerInterface.java - http://pastebin.com/nPxDJgs1