我正在 RMI 中开发一个简单的电话簿应用程序。我能够启动 RMI 注册表,并且还为实现 c1ass 生成存根类。现在我已经在我的 cmd 提示符下使用命令 Java PhoneBookServer 启动了服务器。下一步是启动客户端,所以我启动客户端后出现以下错误!客户端和服务器程序都在一个文件夹中
到目前为止我用于客户端和服务器的代码如下
import java.rmi.*;
import java.rmi.server.*;
public class PhoneBookServer {
public static void main (String[] args){
/*Create and install a security manager
SecurityManager appsm = System.getSecurityManager();
if(appsm==null){
System.setSecurityManager(new RMISecurityManager());
}*/
System.out.println("Server is started");
try
{
//create PhoneBookImpl
PhoneBookImpl Pb=new PhoneBookImpl();
Naming.rebind("rmi://127.0.0.1:1099/PhoneBook", Pb);
}
catch(Exception e)
{
System.out.println("Exception is:" +e);
}
}
}
客户计划
import java.rmi.*;
import java.rmi.registry.*;
import java.rmi.server.*;
import java.lang.*;
import java.io.*;
import java.util.Scanner;
public class PhoneBookClient {
public static void main (String[] args) throws Exception
{
/*Create and install a security manager
SecurityManager appsm = System.getSecurityManager();
if(appsm==null){
System.setSecurityManager(new RMISecurityManager());
}*/
String name,number,total,choice,id;
String ch;
PhoneBook pb=(PhoneBook)Naming.lookup("rmi://127.0.0.1:1099"+"/PhoneBook");
Scanner in=new Scanner(System.in);
System.out.println("1.Enter new record /n");
System.out.println("2.Look up record /n");
System.out.println("3.Delete record /n");
System.out.println("Enter your option");
ch=in.nextLine();
if(ch.equals("1")){
do{
System.out.println("Enter unique id:");
id=in.nextLine();
System.out.println("Enter name:");
name=in.nextLine();
System.out.println("Enter phone number:");
number=in.nextLine();
total=name+" "+number;
pb.new_record(id,total);
System.out.println("Enter 'q' to quit or enter 'p' to proceed");
choice=in.nextLine();
}while(!choice.equals("q"));
}
if(ch.equals("2")){
do{
System.out.println("Enter id to look up a record"+" enter 'q' to quit");
id=in.nextLine();
String record=pb.lookup_record(id);
System.out.println("The record is" +record);
}while(!id.equals("q"));
}
if(ch.equals("2")){
do{
System.out.println("Enter id to delete a record"+" enter 'q' to quit");
id=in.nextLine();
pb.lookup_record(id);
System.out.println("The record is deleted");
}while(!id.equals("q"));
}
}
}
以前我得到了例外:
Connection refused to the host127.0.0.1 access denied.
所以我在我的客户端和服务器程序中安装了安全管理器。现在我得到了这种新型异常。我该如何解决这个问题。