我試圖寫一個簡單的RMI文件,但由於某種原因,它給我的未知的主機名例外。在問一個問題之前,我已經嘗試了一切。這是項目的代碼。Java RMI的的UnknownHostException
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();
}
}
}
Client類:
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();
}
}
}
當我運行這個在本地主機這個運行完全正常,但對於生活我無法通過兩臺機器來運行網絡。我試過了:
- 通過Wifi連接機器。
- 通過以太網連接,因此它們分別具有地址
192.168.1.1
和192.168.1.2
。兩臺機器都可以相互ping通。如果我從客戶端服務器nma -PN
中看到端口1099
處於打開狀態,並且rmiregistry
正在運行。但無論我嘗試使用哪種主機名組合,都不會將客戶端連接到服務器。
組合我試着輸入主機名稱在客戶端:
rmi://192.168.1.1
rmi://192.168.1.1:1099
也許幾十多用所有可能的斜線等仍然不知道爲什麼這不工作(即使它完美通過本地主機)。任何想法都會受到歡迎。
爲了完整這裏是actualy錯誤,我得到:
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
你嘗試只是 '192.168.1.1'?主機名稱是機器的名稱,不應包含「協議」 - 它不是URI/URL。 –
java -classpath classes/Client在這裏我沒有看到任何參數,你傳遞給Client主方法的參數是什麼?在localhost情況下還是在遠程計算機情況下? – zaffargachal
我嘗試了192.168.1.1。正如我所提到的,我現在已經嘗試過所有荒謬的組合,但它們不起作用。 參數不存在,因爲這個錯誤來自不同的運行,我只是簡單地將主機名編碼爲「rmi://192.168.1.2」等,以確保代碼不是問題。 – user1822343