我得到了这个学习RMI的功课。我需要创建一个服务器和多个客户端实例。
每个客户端都连接到服务器,服务器必须保留所有客户端的列表。然后,服务器将所有客户端的网络连接列表返回给每个客户端,这样他们就可以调用彼此的方法,而无需使用服务器。
我得到了每个客户端连接到服务器工作的部分。但我不明白要保留什么以及向客户发送什么,以便他们可以相互沟通。
我正在尝试使用服务器的“registerToBank”方法保留远程引用列表。我远离解决方案吗?
由于
以下是当前代码:
服务器界面
package banqueRmiInterface;
import java.rmi.Remote;
import java.rmi.RemoteException;
import java.util.HashMap;
import utils.SuccursaleInformationsContainer;
public interface BanqueRMIInterface extends Remote {
public int registerToBank(int moneyAmount) throws RemoteException;
}
服务器
package banque;
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.server.RemoteRef;
import java.rmi.server.UnicastRemoteObject;
import java.util.ArrayList;
import java.util.List;
import banqueRmiInterface.BanqueRMIInterface;
import succursale.Succursale;
import utils.SuccursaleInformationsContainer;
public class Banque extends UnicastRemoteObject implements BanqueRMIInterface{
private static final long serialVersionUID = 1L;
int totalMoney = 0;
List<Succursale> remoteObjectList = new ArrayList<Succursale>();
protected Banque() throws RemoteException {
super();
}
@Override
public int registerToBank(int moneyAmount, Succursale succursale) throws RemoteException{
succursale.setId(remoteObjectList.size());
remoteObjectList.add(succursale);
totalMoney += moneyAmount;
System.out.println(" succurcale #" + succursale.getId() + " added " + moneyAmount + "$ to the lot");
System.out.println("New total of money is: " + totalMoney);
updateClientsRemoteObjectList();
return remoteObjectList.size()-1;
}
public static void main(String[] args){
try {
startRmiRegistry();
Naming.rebind("//127.0.0.1/Bank", new Banque()); //Change this for your own ip
System.out.println("Server ready");
} catch (Exception e) {
System.err.println("Server exception: " + e.toString());
e.printStackTrace();
}
}
public void updateClientsRemoteObjectList(){
for(int i = 0; i < remoteObjectList.size(); i++){
System.out.println("updating client #" +remoteObjectList.get(i).getId() );
remoteObjectList.get(i).updateRemoteObjectList(remoteObjectList);
}
}
public static void startRmiRegistry(){
try {
java.rmi.registry.LocateRegistry.createRegistry(1099);
System.out.println("RMI registry ready.");
} catch (Exception e) {
System.out.println("Exception starting RMI registry:");
e.printStackTrace();
}
}
}
客户:
public class Succursale implements Serializable{
/**
*
*/
private static final long serialVersionUID = -905645444505287895L;
private BanqueRMIInterface look_up;
private int id = 0;
private List<Succursale> remoteObjectList;
public static void main(String[] args) throws RemoteException, MalformedURLException, NotBoundException, Exception{
Succursale test = new Succursale();
test.doThings();
}
public void doThings() throws RemoteException, MalformedURLException, NotBoundException, Exception{
look_up = (BanqueRMIInterface) Naming.lookup("//127.0.0.1/Bank"); //Change this for your own ip
System.out.println("Combien d'argent vous avez?");
BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
String userInput;
int moneyAmount =0;
if ((userInput = stdIn.readLine()) != null)
{
System.out.println(userInput);
moneyAmount = Integer.parseInt(userInput);
}
id = look_up.registerToBank(moneyAmount, this);
while(true)
{
//do things
}
}
public void updateRemoteObjectList(List<Succursale> remoteObjectList){
this.remoteObjectList = remoteObjectList;
}
public int getId(){
return this.id;
}
public void setId(int id){
this.id = id;
}
}
答案 0 :(得分:-1)
答案在你的标题中。 &#39;随着他们的参考&#39;。所有客户端必须从服务器获取对另一个客户端的远程引用,然后以正常方式开始使用它。
防火墙允许。
答案 1 :(得分:-1)
每个客户如何相互沟通? 如果他们通过RMI进行通信,那么您必须从每个客户端公开RMI接口。并且您无法将网络连接返回到远程服务器,以便在不同的服务器(不同位置)中使用!