Hello everyone,
I have been banging my head really hard trying to solve this problem. I really appreciate if anyone can please have a look at my problem and help me.
I have multiple clients that uses Java RMI to establish a connection (register()) and receive a list of connects clients from the server (get()). My goal is to get clients to talk to each other using RMI, without having to register themselves, only the server so far is being register.
I am trying to pass a reference of the client object to the server.
I am getting the following error:
java.rmi.MarshalException: error marshalling arguments; nested exception is:
java.io.NotSerializableException: Client
I do not want to use Serialization (I believe), as I don't want to pass the object itself, but a reference. Client1 should be able to send a message to Client2 by calling client2.messsage(String username, String message);
I believe I should just show you at this point how I have implemented my code:
public interface RMIClientIntf extends Remote {
public void message(String name, String message) throws RemoteException;
public String getName() throws RemoteException;
}
The previous code is what all clients should implement. If the message() function gets called from another client, that means the other class is sending a message.
My client class itself:
public class Client implements RMIClientIntf {
public Client() throws RemoteException { super(); }
public String getName() throws RemoteException { }
}
Now, the Main class that creates an instance of client calls the following, to send the remote object to the server:
final Client client = new Client();
server.register((RMIClientIntf) client, name);
On the server side, the register method is defined as:
public interface RMIServerIntf extends Remote {
public String register(RMIClientIntf cl, String userName) throws RemoteException;
public RMIClientIntf[] get() throws RemoteException;
}
public class Server extends UnicastRemoteObject implements RMIServerIntf {
public String register(RMIClientIntf cl, String userName) throws RemoteException {
ClientStruct newClient = new ClientStruct(cl, userName);
/* Store the ClientStruct */
return new String("SUCCESS");
}
}
After registering, each client will request the list of connected users. The function in the server is the following:
public RMIClientIntf[] get() throws RemoteException {
RMIClientIntf[] users = new RMIClientIntf[openConnections.size()];
/* Fill in users from ClientStruct */
return users;
}
If Client were to also implement Serializable, then the program runs but when client2 calls client1.message(), the message() method gets called for client2 and throws a NullPointer at this point.
I was looking at this link: http://www.exampledepot.com/egs/java.rmi/args_Args.html, which gave me a hint that it should implement Remote but not Serialization to pass by reference. I am not sure why the program is complaining in my case.
I really appreciate any help I can get. I've been trying to fix this problem for a while and I can't seem to find any solution.
Thank you very much!