我很难找到这个问题的明确答案,所以我想我会在这里用自己的具体例子问:
我正在创建一个mulitplayer垄断游戏.实际的垄断代码在服务器上运行,客户端本质上是一个访问和控制此代码的GUI.垄断游戏由一个名为"银行"的类控制.
假设我在客户端的main()中执行了此操作:
Bank banker = server.getBank(); //gets the bank object from server
bank.turn(); //moves the current player
Run Code Online (Sandbox Code Playgroud)
这会调用服务器上的Bank对象上的turn()还是本地机器上的它的副本?
更新:银行没有实现远程.它是一个可序列化的对象.
我试图在RMI方法中添加参数.当我添加例如String一切正常.但我不确定我是否可以传递我创建的对象.我是RMI的新手,所以我的代码非常简单:
HelloIF
public interface HelloIF extends Remote {
String greeting(Context c) throws RemoteException;
}
Run Code Online (Sandbox Code Playgroud)
你好
public class Hello extends UnicastRemoteObject implements HelloIF {
public Hello() throws RemoteException {
}
public String greeting(Context c) throws RemoteException {
addToContext(c);
report(c);
return "greeting";
}
void addToContext(Context c) {
c.addID(Thread.currentThread().getId());
}
void report(Context c) {
System.out.println("Hello.greeting() thread : "
+ Thread.currentThread().getName() + " "
+ Thread.currentThread().getId());
System.out.println("Hello.greeting() context : "
+ c.getDistributedThreadName() + " " + c.getRequestType());
}
}
Run Code Online (Sandbox Code Playgroud)
为RMIServer
public class …Run Code Online (Sandbox Code Playgroud)