我有一个简单的RMI - 客户端,服务器架构。因为我想从我在接口之后定义的服务器做回调:
public interface Meiern_RMI_Server extends Remote{
public boolean registerClient(RMIClient client) throws RemoteException;
}
public interface Meiern_RMI_Client extends Remote{
public void setTurn(boolean yourTurn) throws RemoteException;
}
这里是Impls(服务器):
public class RMIServer extends UnicastRemoteObject implements Meiern_RMI_Server{
private Set<RMIClient> clients;
public RMIServer() throws RemoteException {
super();
this.clients = new HashSet<RMIClient>();
}
public static void main(String [] args ){
try {
Registry reg = LocateRegistry.createRegistry(1234);
reg.rebind("server", new RMIServer());
System.out.println("server started");
} catch(Exception e) {
e.printStackTrace();
}
}
@Override
public synchronized boolean registerClient(RMIClient client) throws RemoteException {
boolean registered = false;
try {
registered = clients.add(client);
if(registered) {
System.out.println("Client " + client.getName() + " registered.");
System.out.println(clients);
} else {
System.out.println("Could not register client. Duplicate names " + client.getName());
}
} catch (Exception e) {
e.printStackTrace();
}
return registered;
}
}
客户端:
public class RMIClient extends UnicastRemoteObject implements Meiern_RMI_Client { public RMIClient() throws RemoteException {
super();
}
@Override
public void setTurn(boolean yourTurn) throws RemoteException {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
public static void main(String [] args) {
try {
RMIClient client = new RMIClient();
Registry reg = LocateRegistry.getRegistry("127.0.0.1", 1234);
Meiern_RMI_Server rmi = (Meiern_RMI_Server)reg.lookup("server");
System.out.println("client connected");
boolean registered = rmi.registerClient(client);
if(registered){
System.out.println("Client registered");
} else {
System.out.println("Could not register client, Change name");
}
while (true) {
}
} catch (Exception e) {
System.out.println("Could not connect to server: ");
e.printStackTrace();
}
}
}
尝试注册客户端时,客户端发生以下异常:
java.lang.IllegalArgumentException: argument type mismatch
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:322)
at sun.rmi.transport.Transport$1.run(Transport.java:177)
at sun.rmi.transport.Transport$1.run(Transport.java:174)
at java.security.AccessController.doPrivileged(Native Method)
at sun.rmi.transport.Transport.serviceCall(Transport.java:173)
at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:556)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:811)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:670)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:745)
at sun.rmi.transport.StreamRemoteCall.exceptionReceivedFromServer(StreamRemoteCall.java:275)
at sun.rmi.transport.StreamRemoteCall.executeCall(StreamRemoteCall.java:252)
at sun.rmi.server.UnicastRef.invoke(UnicastRef.java:161)
at java.rmi.server.RemoteObjectInvocationHandler.invokeRemoteMethod(RemoteObjectInvocationHandler.java:194)
at java.rmi.server.RemoteObjectInvocationHandler.invoke(RemoteObjectInvocationHandler.java:148)
at com.sun.proxy.$Proxy1.registerClient(Unknown Source)
at meiern.RMIClient.main(RMIClient.java:97)
有什么建议吗?
答案 0 :(得分:1)
public boolean registerClient(RMIClient client) throws RemoteExceptio
您不能将导出的远程对象类型用作远程方法参数或返回类型。将其更改为:
public boolean registerClient(Myeiern_RMI_Client client) throws RemoteException