2013-10-29 59 views
0

enter image description here我正在RMI中開發一個簡單的電話簿應用程序。我能夠啓動RMI註冊表,併爲實現c1ass生成存根類。現在我已經在我的cmd提示符下使用命令Java PhoneBookServer啓動了服務器。下一步是啓動客戶端,所以在我啓動客戶端後出現以下錯誤!客戶端和服務器程序都在一個文件夾中enter image description here客戶端如何在Java RMI中連接到服務器?

我用到目前爲止了客戶端和服務器的代碼如下所示

 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. 

所以我安裝安全mnager在我的cl客戶和服務器程序。現在我得到這種新的例外。我該如何解決這個問題。

+0

您可以通過點擊上面問題中的「enter image description」在cmd提示符中看到錯誤 –

+0

什麼新的異常?什麼問題?發佈例外和代碼*在這裏*大多數人不會打擾跟隨鏈接,並且該問題沒有永久的價值,除非它包含所有相關材料。 – EJP

+0

http://stackoverflow.com/questions/2427473/java-rmi-accesscontrolexception-access-denied –

回答

0

擺脫安全經理。在這種配置中你不需要它。擺脫安全管理器安裝在服務器上:你只能安裝一個;並在客戶端擺脫它。

我不明白這兩個安全管理器可能如何使用該服務器代碼。這不可能是真正的代碼。

+0

我在客戶端和服務器中刪除了一個安全管理器。現在我得到一個異常,當我啓動服務器:java PhoneBookServer'java.security.AccessControlException:訪問被拒絕 (java.net.SocketPermission 127.0.0.1:1099連接,解決)' –

+0

Yon只能得到該異常,如果有安裝經理安裝。我確實特別說要在服務器中刪除*兩個*。 – EJP

+0

首先我嘗試了沒有任何安全管理器,然後服務器顯示錯誤訪問被拒絕,然後我安裝了安全管理器,以便它可能連接,但它仍然顯示相同的異常 –

相關問題