這是我第一次使用RMI,基本上我在本地PC上運行以下RMI示例,但不能通過兩臺獨立的Linux機器運行。rmi - ConnectException
服務器接口:
public interface PowerService extends Remote{
public BigInteger square (int number)
throws RemoteException;
public BigInteger power (int num1, int num2)
throws RemoteException;
}
服務器:
public class PowerServiceServer extends UnicastRemoteObject implements
PowerService {
public PowerServiceServer() throws RemoteException {
super();
}
public BigInteger square(int number) throws RemoteException {
imp .....
return (bi);
}
public BigInteger power(int num1, int num2) throws RemoteException {
imp .....
return bi;
}
public static void main(String[] args) throws Exception {
PowerServiceServer svr = new PowerServiceServer();
// ... and bind it with the RMI Registry
Naming.bind("PowerService", svr);
System.out.println("Service bound....");
}
}
客戶端:
public class PowerServiceClient {
public static void main(String args[]) throws Exception {
// Call registry for PowerService
PowerService service = (PowerService) Naming.lookup("rmi://" + args[0]
+ "/PowerService");
DataInputStream din = new DataInputStream(System.in);
for (;;) {
System.out.println("1 - Calculate square");
System.out.println("2 - Calculate power");
System.out.println("3 - Exit");
System.out.println();
System.out.print("Choice : ");
String line = din.readLine();
Integer choice = new Integer(line);
int value = choice.intValue();
switch (value) {
case 1:
// Call remote method
....................
break;
case 2:
// Call remote method
....................
break;
case 3:
System.exit(0);
default:
System.out.println("Invalid option");
break;
}
}
}
和客戶機接口是像服務器的同一
這是什麼 我沒有爲了運行RMI例如:
1)在服務器端我創建存根
2)執行命令rmiregisrty
3)運行服務器
4)我複製從服務器端向客戶端存根到同一個包
5)運行客戶端
運行客戶端後,我得到了以下錯誤消息:
線程「main」中的異常java.rmi.ConnectException:連接拒絕主機:127.0.0.1;嵌套的異常是: java.net.ConnectException:連接被拒絕 at sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:601) 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.invoke(UnicastRef.java:110) at compute.PowerServiceServer_Stub.square(Unknown來源)
是可能的,由於某些防火牆我無法連接或者也許我做錯了什麼?
感謝
是127.0.0.1您希望連接到的IP地址?換句話說,客戶端是否與服務器在同一主機上運行?注意你的遠程方法*實現*不必被聲明爲拋出'RemoteException',除非編譯器堅持,只有當它們調用它們自己的遠程方法時纔會發生,這是非常罕見的。 – EJP
沒有IP 127.0.0.1不是我期待看到的,因爲客戶端在不同的機器上(ip) – angus