在 java RMI 中,我正在构建一个聊天应用程序。但我无法找到一种方法来确定访问我服务器的 IP 是来 self 的内部组织网络(INTRANET)还是来自外部世界(INTERNET)。 现在我正在使用
try {
System.setProperty("java.rmi.server.hostname",InetAddress.getLocalHost().getHostAddress());
Registry statusRegistry = LocateRegistry.createRegistry(ChatConstants.statusPort);
ChatInterface chat = new ChatImpl(ChatConstants.statusPort) ;
statusRegistry.rebind("statusconnection",chat);
System.out.println("RMIStatusConnection Server is started...");
} catch (RemoteException e) {
System.out.println("RMIStatusConnection failed...");
e.printStackTrace();
} catch (UnknownHostException e) {
e.printStackTrace();
}
最佳答案
尝试RemoteServer.getClientHost(),但它可能只提供最近的NAT设备的地址。
注意,这样设置 java.rmi.server.hostname 没有多大意义。这是默认设置。仅当默认设置有问题时才需要进行设置。
关于java RMI + 源命中服务器来自互联网或来自内网,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15804977/