[英]i have a problem in a java rmi multiclient code
我正在編寫一個 java rmi multi_client 代碼,其中第一個客戶端 (client1) 有權修改或創建一個對象,而 succond 客戶端 (client2) 有權訪問由 client1 創建的對象返回 onject 以查看它,但它不起作用第一類客戶
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.List;
public class ChefDeDepartement {
static Scanner sc1 = new Scanner(System.in);
static Scanner sc2 = new Scanner(System.in);
static Scanner sc3 = new Scanner(System.in);
public static void main(String[] args)throws RemoteException, NotBoundException, MalformedURLException {
int i=0;
try
{
//java.rmi.registry.LocateRegistry.createRegistry(1000);
InterfaceChefDept c = (InterfaceChefDept)Naming.lookup("rmi://localhost/ImlementationInterfaceChefDept");
System.out.println("connection au serveur");
c.Ajouter("1ere avis");
c.Ajouter("2eme avis");
c.Ajouter("3eme avis");
List<String> avis = c.returnArrayList();
System.out.println("Bienvenu !!!!!!!!!!");
while(i==0){
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
//affichage*************************************************************
System.out.println("La liste des avis :");
List<String> listAvis = avis;
int j=0;
int k=0;
for(String e: listAvis){
j=j+1;
System.out.println(j + " :" + e);
}
//affichage*************************************************************
System.out.println("*********************");
System.out.println("**********");
System.out.println("choisire votre Action :");
System.out.println(" 1: Ajouter un avis \n");
System.out.println(" 2: Supprimer \n");
int choice = sc1.nextInt();
String newAvis;
int idAvis;
if (choice==1)
{
System.out.println("donner le nouvel avis : ");
newAvis = sc2.nextLine();
c.Ajouter(newAvis);
avis = c.returnArrayList();
/*
//affichage*************************************************************
System.out.println("La liste des avis :");
int l=0;
for(String f: avis){
l=l+1;
System.out.println(l + " :" + f);
}
//affichage*************************************************************
*/
}
if (choice==2)
{
System.out.println("donner l'indice de l'avis a supprimer ");
idAvis = sc3.nextInt();
System.out.println("scan done");
c.Supprimer(idAvis);
System.out.println("supp done ");
avis = c.returnArrayList();
/*
//affichage*************************************************************
System.out.println("La liste des avis :");
int n=0;
for(String f: avis){
n=n+1;
System.out.println(n + " :" + f);
}
//affichage*************************************************************
*/
}
}
}
catch(Exception e)
{
System.out.println(e);
}
}
}
succond 客戶類
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.List;
public class Etudiant {
public static void main(String [] args) throws RemoteException {
try{
int i=0;
InterfaceChefDept c = (InterfaceChefDept) Naming.lookup("rmi://localhost/ImlementationInterfaceChefDept");
InterfaceEtudiant s = (InterfaceEtudiant) Naming.lookup("rmi://localhost/ServerDesAvis");
List<String> avis = c.returnArrayList();
List<String> lavis = s.AfficherAvis(avis);
System.out.println("La liste des avis disponibles :");
for(String f: lavis){
i=i+1;
System.out.println(i + " : " + f);
}
System.out.println(":) :) :) :) :) ");
}
catch(Exception e){
e.printStackTrace();
}
}
}
服務器類
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.NotBoundException;
import java.rmi.Remote;
import java.util.ArrayList;
public class ServerDesAvis {
public static void main(String[] args) throws RemoteException,NotBoundException{
try
{
ImlementationInterfaceChefDept c = new ImlementationInterfaceChefDept();
Naming.rebind("rmi://localhost/ServerDesAvis", c);
ImplementationInterfaceEtudiant s = new ImplementationInterfaceEtudiant();
Naming.rebind("rmi://localhost/ServerDesAvis", s);
System.out.println("server is running");
System.out.println(".......");
System.out.println(".....");
System.out.println("...");
}
catch(RemoteException re){
re.printStackTrace();
}
catch(Exception e) {
System.out.println(e);
}
}
}
在java ChefDepartement localhost 之后,我收到此錯誤 java.rmi.NotBoundException: ImmentationInterfaceChefDept
您的ImlementationInterfaceChefDept
RMI 服務器未正確注冊。 您將兩者綁定到同一個ServerDesAvis
端點:
ImlementationInterfaceChefDept c = new ImlementationInterfaceChefDept();
Naming.rebind("rmi://localhost/ServerDesAvis", c);
ImplementationInterfaceEtudiant s = new ImplementationInterfaceEtudiant();
Naming.rebind("rmi://localhost/ServerDesAvis", s);
...這意味着第一個( ImlementationInterfaceChefDept
)將被第二個覆蓋。 你需要更換:
ImlementationInterfaceChefDept c = new ImlementationInterfaceChefDept();
Naming.rebind("rmi://localhost/ServerDesAvis", c);
... 有了這個:
ImlementationInterfaceChefDept c = new ImlementationInterfaceChefDept();
Naming.rebind("rmi://localhost/ImlementationInterfaceChefDept", c);
希望這可以幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.