Java RMI在上使用时创建连接时发生异常!=机器



我已经为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}接口。我从RMISRegistry获取服务器对象并调用登录方法没有问题。我可以通过服务器端的断点看到代码被执行,但一旦到达回调指令,它就会失败。客户端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;
}

这是典型的java.rmi.server.hostname问题。参见RMI常见问题解答中的A.1项。您需要将服务器JVM上的java.rmi.server.hostname设置为您希望客户端在连接时使用的地址。这个问题是由错误配置的DNS引起的:也许你可以在那里解决它:如果可以的话会更好。"localhost’应该解析为127.0.0.1,并且您的主机名应该解析为您的真实IP地址。

相关内容

最新更新