我现在一直在努力解决RMI问题几个小时。它是一个非常愚蠢的例子,现在只有当我启动客户端和放大器时才有效。服务器在同一台计算机上。一旦我在!=机器上启动客户端^服务器,当服务器尝试调用回调时我会收到异常:
Exception creating connection to: 192.168.244.1; nested exception is:
java.net.SocketException: Network is unreachable
at sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:632)
at sun.rmi.transport.tcp.TCPChannel.createConnection(TCPChannel.java:216)
at sun.rmi.transport.tcp.TCPChannel.newConnection(TCPChannel.java:202)
at sun.rmi.server.UnicastRef.invoke(UnicastRef.java:128)
顺便说一下,因为我在10.0.0.x子网上,所以这个IP来自哪里?
代码由两个类组成,Server&客户端,实现两个{Server | Client}接口。从RMIRegistry获取服务器对象并调用login方法没有问题。我可以在服务器端看到一个断点,即代码被执行,但是一旦它返回到回调指令就失败了。客户端btw是个例外。
我尝试在两台机器上禁用防火墙但没有成功。我怀疑问题来自异常发送给我的ip,但我无法弄清楚它来自何处。
以下代码如下: (Client.java)
public class Client extends UnicastRemoteObject implements ClientInterface {
public static void main(String[] args) {
try {
new Client();
} catch (RemoteException e) {
e.printStackTrace();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (NotBoundException e) {
e.printStackTrace();
}
}
public Client() throws RemoteException, MalformedURLException, NotBoundException {
ServerInterface s = null;
s = (ServerInterface) Naming.lookup("rmi://10.0.0.48/server");
s.login(this);
}
@Override
public void talk() throws RemoteException {
System.out.println("Talkiiiing");
}
}
(ClientInterface.java)
public interface ClientInterface extends Remote {
void talk() throws RemoteException;;
}
(Server.java)
public class Server extends UnicastRemoteObject implements ServerInterface {
public Server() throws RemoteException, MalformedURLException {
LocateRegistry.createRegistry(1099);
if (System.getSecurityManager() == null)
System.setSecurityManager(new RMISecurityManager());
Naming.rebind("rmi://localhost:1099/server", this);
}
public static void main(String[] args) {
try {
new Server();
} catch (RemoteException e) {
e.printStackTrace();
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
@Override
public void login(ClientInterface clientInterface) throws RemoteException {
clientInterface.talk();
}
}
最后是(ServerInterface.java)
public interface ServerInterface extends Remote {
void login(ClientInterface clientInterface) throws RemoteException;
}
答案 0 :(得分:1)
这是经典的java.rmi.server.hostname问题。见item A.1 in the RMI FAQ。您需要将服务器JVM上的java.rmi.server.hostname设置为您希望客户端在连接时使用的地址。问题是由错误配置的DNS引起的:也许您可以在那里修复它:如果可以,那就更好了。 '本地主机'应解析为127.0.0.1,您的主机名应解析为您的真实IP地址。