运行rmi测试程序的错误

时间:2012-07-20 05:40:05

标签: java rmi

我正在尝试运行以下两个类,但是我收到了这个错误 有谁知道问题是什么?

java.rmi.ConnectException: Connection refused to host: 10.0.0.2; nested exception is: 
    java.net.ConnectException: Connection refused: connect
    at sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:619)
    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.newCall(UnicastRef.java:340)
    at sun.rmi.registry.RegistryImpl_Stub.rebind(Unknown Source)
    at RegisterWithRMIServer.main(RegisterWithRMIServer.java:9)
Caused by: java.net.ConnectException: Connection refused: connect
    at java.net.DualStackPlainSocketImpl.connect0(Native Method)
    at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:69)
    at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339)
    at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)
    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:157)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:391)
    at java.net.Socket.connect(Socket.java:579)
    at java.net.Socket.connect(Socket.java:528)
    at java.net.Socket.<init>(Socket.java:425)
    at java.net.Socket.<init>(Socket.java:208)
    at sun.rmi.transport.proxy.RMIDirectSocketFactory.createSocket(RMIDirectSocketFactory.java:40)
    at sun.rmi.transport.proxy.RMIMasterSocketFactory.createSocket(RMIMasterSocketFactory.java:146)
    at sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:613)
    ... 5 more




import java.rmi.registry.*; 

public class RegisterWithRMIServer { 
    /** Main method */ 
    public static void main(String[] args) { 
      try { 
        StudentServerInterface obj =  new StudentServerInterfaceImpl(); 
        Registry registry = LocateRegistry.getRegistry(); 
        registry.rebind("StudentServerInterfaceImpl", obj); 
        System.out.println("Student server " + obj +  " registered"); 
     } 
     catch (Exception ex) { 
       ex.printStackTrace(); 
     } 
   } 
 }

///////

import java.rmi.*; 
import java.rmi.server.*; 
import java.util.*; 

public class StudentServerInterfaceImpl  extends UnicastRemoteObject  
      implements StudentServerInterface { 
    // Stores scores in a map indexed by name 
        private HashMap<String, Double> scores =  
         new HashMap<String, Double>(); 

   public StudentServerInterfaceImpl() throws RemoteException { 
     initializeStudent(); 
   } 

   /** Initialize student information */ 
   protected void initializeStudent() { 
     scores.put("John", new Double(90.5)); 
     scores.put("Michael", new Double(100)); 
     scores.put("Michelle", new Double(98.5)); 
   } 

   /** Implement the findScore method from the  
    *  Student interface */ 
   public double findScore(String name) throws RemoteException { 
     Double d = (Double)scores.get(name); 

     if (d == null) { 
       System.out.println("Student " + name + " is not found "); 
       return -1; 
     } 
     else { 
     System.out.println("Student " + name + "\'s score is " + d.doubleValue()); 
       return d.doubleValue(); 
     } 
   } 
}

1 个答案:

答案 0 :(得分:2)

您是否启动了注册表。以下是RMI的简要链接

RMI Tutorial (Running RMI programs)

要在Windows上启动注册表(如果您的操作系统是Windows),您需要打开命令提示符并输入

  

启动rmiregistry

否则,如果您的操作系统是Solaris或Linux

  

rmiregistry&amp;