RMI的一个简单测试疑惑
定义了一个remote接口
public interface HelloService extends Remote {
//服务器端定义的服务接口
public abstract String echo(String msg) throws RemoteException;
public abstract Date getTime() throws RemoteException;
}
//下面是服务器端的程序,其中HelloServiceImpl1 ,HelloServiceImpl2 是二个HelloService的实现类且都继承了//UnicastRemoteObject类。
public class SimpleServer {
/**
* @param args
*/
public static void main(String[] args) throws RemoteException{
// TODO Auto-generated method stub
try {
HelloServiceImpl1 helloService1=new HelloServiceImpl1();
HelloServiceImpl2 helloService2=new HelloServiceImpl2();
Context namingContext=new InitialContext(System.getProperties());
namingContext.rebind("rmi://localhost:8000/helloService1", helloService1);
namingContext.rebind("rmi://localhost:8000/helloService2", helloService2);
System.out.println("服务器注册了2个HelloService对象");
} catch (NamingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
运行完毕后出现异常:
javax.naming.CommunicationException [Root exception is java.rmi.ConnectIOException: Exception creating connection to: localhost; nested exception is:
java.net.SocketException: Network is unreachable: connect]