我正在尝试编写一个简单的RMI文件,但由于某种原因,它给了我一个未知主机名异常。在实际提出问题之前,我已经尝试了所有的东西。这是项目的代码。
RMI接口类:
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface Hello extends Remote {
String sayHello() throws RemoteException;
}
服务器类:
import java.rmi.registry.Registry;
import java.rmi.registry.LocateRegistry;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
public class Server implements Hello {
public Server() {}
public String sayHello() {
return "Hello, world!";
}
public static void main(String args[]) {
try {
Server obj = new Server();
Hello stub = (Hello) UnicastRemoteObject.exportObject(obj, 0);
// Bind the remote object's stub in the registry
Registry registry = LocateRegistry.getRegistry();
registry.bind("Hello", stub);
System.err.println("Server ready");
} catch (Exception e) {
System.err.println("Server exception: " + e.toString());
e.printStackTrace();
}
}
}
客户端类:
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
public class Client {
private Client() {}
public static void main(String[] args) {
String host = (args.length < 1) ? null : args[0];
try {
Registry registry = LocateRegistry.getRegistry(host);
Hello stub = (Hello) registry.lookup("Hello");
String response = stub.sayHello();
System.out.println("response: " + response);
} catch (Exception e) {
System.err.println("Client exception: " + e.toString());
e.printStackTrace();
}
}
}
当我在localhost上运行时,这运行完全正常但是对于我的生活,我无法通过两台机器在网络上运行。我试过了:
192.168.1.1
和192.168.1.2
等地址。两台机器都可以互相ping通。如果我nma -PN
来自客户端的服务器,我可以看到端口1099
已打开且rmiregistry
正在运行。但无论我尝试使用哪种主机名组合,它仍然不会将客户端连接到服务器。组合我尝试在客户端输入主机名:
rmi://192.168.1.1
rmi://192.168.1.1:1099
也许还有几十个所有可能的斜线等仍然不知道为什么这不起作用(即使它完全适用于localhost)。任何想法都会受到欢迎。
为了完整性,这里是我得到的实际错误:
java -classpath classes/ Client
Client exception: java.rmi.UnknownHostException: Unknown host: rmi://192.168.1.1; nested exception is:
java.net.UnknownHostException: rmi://192.168.1.1
java.rmi.UnknownHostException: Unknown host: rmi://192.168.1.1; nested exception is:
java.net.UnknownHostException: rmi://192.168.1.1
at sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:598)
at sun.rmi.transport.tcp.TCPChannel.createConnection(TCPChannel.java:198)
at sun.rmi.transport.tcp.TCPChannel.newConnection(TCPChannel.java:184)
at sun.rmi.server.UnicastRef.newCall(UnicastRef.java:322)
at sun.rmi.registry.RegistryImpl_Stub.lookup(Unknown Source)
at Client.main(Client.java:14)
Caused by: java.net.UnknownHostException: rmi://192.168.1.1
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:195)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
at java.net.Socket.connect(Socket.java:529)
at java.net.Socket.connect(Socket.java:478)
at java.net.Socket.<init>(Socket.java:375)
at java.net.Socket.<init>(Socket.java:189)
at sun.rmi.transport.proxy.RMIDirectSocketFactory.createSocket(RMIDirectSocketFactory.java:22)
at sun.rmi.transport.proxy.RMIMasterSocketFactory.createSocket(RMIMasterSocketFactory.java:128)
at sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:595)
... 5 more
答案 0 :(得分:2)
这已经解决了。我将发布自己的解决方案。显然运行这样的服务器工作:java -Djava.rmi.server.hostname = 192.168.1.1 Server。问题是,当客户端调用服务器并获取存根时,此存根包含此属性,并且java将此属性设置为默认情况下为localhost,即使在创建注册表时将服务器绑定到ip也是如此。这就是为什么即使你的客户端连接到一个远程主机,它崩溃说在本地主机上拒绝连接,因为它的存根包含localhost。
答案 1 :(得分:0)
只是对主机名和IP之间的映射进行评论:您可以随意命名定位器,但是您必须在主机文件中插入一个条目(C:\ Windows \ system32 ... \ hosts),例如
127.0.0.1 rmihost1
然后你可以在电话中使用rmihost1:
LocateRegistry.getRegistry(host, port);