我们正试图让RMI在互联网上运作。我们尝试了什么:
我们的RMI界面:
import java.rmi.RemoteException;
public interface RMIInterface extends java.rmi.Remote {
public void helloWorld(int i) throws RemoteException;
}
我们的RMI服务器实施:
import java.net.MalformedURLException;
import java.rmi.AlreadyBoundException;
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;
public class RMIServerTest extends UnicastRemoteObject implements RMIInterface {
public RMIServerTest() throws RemoteException {
}
@Override
public void helloWorld(int i) throws RemoteException {
for (int j = 0; j < i; j++) {
System.out.println("Hello World");
}
}
public static void main(String[] args) {
try {
LocateRegistry.createRegistry(Registry.REGISTRY_PORT);
}
catch (RemoteException ex) {
System.out.println(ex.getMessage());
}
try {
Naming.rebind("Server", new RMIServerTest());
} catch (MalformedURLException ex) {
System.out.println(ex.getMessage());
} catch (RemoteException ex) {
System.out.println(ex.getMessage());
}
}
}
和我们的客户:
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
public class RMIClient {
public static void main(String[] args) throws RemoteException, NotBoundException,MalformedURLException {
try {
RMIInterface serverObject = (RMIInterface) Naming.lookup("//externalServerAdress/Server");
serverObject.helloWorld(10);
}
catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
我们仍然收到此错误:
Connection refused to host: 192.168.0.13; nested exception is:
java.net.ConnectException: Connection timed out: connect
192.168.0.13是路由器后面的服务器的本地IP地址。我们在客户端上连接路由器的外部IP。比如“2.246.133.155”= externalServerAdress。 所以我们有联系。我们通过服务器的外部IP地址(WAN IP)连接并显示错误,它获取服务器的本地IP地址,但仍然拒绝连接。
thx任何提示。
答案 0 :(得分:0)
Connection refused to host: 192.168.0.13
这不是互联网地址。它是一个私有地址,仅存在于路由器后面。您需要使用公共 IP地址,并通过路由器安排端口转发。