当前,我正在尝试在远程计算机上使用RMI启动Java服务器。服务器似乎已启动,但是我无法在客户端上调用其方法。 在客户端上,我得到一个异常,看起来像这样:
Exception in thread "main" java.rmi.ConnectException: Connection refused to host: 10.0.0.6; nested exception is:
java.net.ConnectException: Operation timed out (Connection timed out)
这是我的RMI Server构造函数:
private RmiServer(String address, int port) throws RemoteException, SQLException, MalformedURLException {
System.out.println("This address = " + address + ", Port = " + port);
Registry registry;
try {
registry = LocateRegistry.createRegistry(PORT);
} catch (RemoteException e)
{
registry = LocateRegistry.getRegistry(PORT);
}
System.setProperty("java.rmi.server.hostname", address);
try {
registry.bind(REGISTRY_NAME, this);
Naming.bind(address, this);
}
catch (AlreadyBoundException e)
{
registry.rebind(REGISTRY_NAME, this);
Naming.rebind(address, this);
}
...
}
这是我的客户代码:
static public void main(String[] args) throws IOException {
try {
Registry registry = LocateRegistry.getRegistry(SERVER_ADDRESS, SERVER_PORT);
rmiServer = (ReceiveMessageInterface) registry.lookup(REGISTRY_NAME);
} catch (RemoteException | NotBoundException e) {
System.err.println(e.getMessage());
isServerOK = false;
}
Scanner reader = new Scanner(new InputStreamReader(System.in));
boolean isAuthenticated = false;
int pin;
String cardNumber;
SessionInfo session = null;
if (isServerOK) {
while (true) {
if (!isAuthenticated) {
System.out.println("Welcome Customer to HDFC Bank ATM : \nPlease enter your card number: ");
cardNumber = reader.next();
System.out.println("Please enter your PIN: ");
pin = reader.nextInt();
String cardHolderName = rmiServer.verifyPIN(cardNumber, pin);
...
}
...
}
...
}
...
}
我用以下字符串启动服务器:
java -cp flyway-core-6.0.8.jar:mssql-jdbc-7.4.1.jre8.jar:mysql-connector-java-8.0.18.jar:. -Djava.security.policy=java.security.AllPermission kmalfa.RmiServer
我的服务器在linux虚拟机上运行。我使用Microsoft Azure服务。我关闭了客户端和虚拟机上的防火墙。我允许Azure设置中的所有传入连接用于测试目的,但无济于事。 可能是什么问题?
答案 0 :(得分:0)
已解决。
只需要打电话
System.setProperty("java.rmi.server.hostname", address);
不在 RmiServer 构造函数中,但是在调用它之前,位于 main
的开头