Returning a Remote object at client login.

Hello.

I have the feeling that this is a basic question which might be asked more often. But strangely I didn't find this issue in the basic RMI tutorials and a google search didn't give me anything useful too.

I am making a client/server program with RMI running on java 1.6.0. On the server I make a registry with Registry.createRegistry(1099) (I hate using the rmiregistry program) and bind a UniCastRemoteObject called

LoginControllerImpl. This class implements a Remote interface LoginController with a single login method. I wish each client to receive its own Remote object on login to communicate with the server.

The implementation currently looks like this:

public ClientControls login(String userName, String password)throws RemoteException{

// TODO: validate userName and password

System.out.println("Client " + userName +" logged in");

ClientControls c =new ClientControlsImpl(userName);

// Do I need this?UnicastRemoteObject.exportObject(c);

return c;

}

Now clients can call the login method and the login code on the server is executed (I know because the server prints out the message)

For each client that logs in I would like to return a Remote object ClientControls to the client with which the client can send requests to the server.

ClientControls looks like this:

publicinterface ClientControlsextends Remote{

publicvoid sayHi();

}

The server's implementation looks like this:

publicclass ClientControlsImplimplements Remote, ClientControls{

private String userName;

public ClientControlsImpl(String userName)throws RemoteException{

this.userName = userName;

}

publicvoid sayHi(){

System.out.println(userName +" says hi.");

}

Now when I run this I get an UnmarshalException with nested exception NotSerializableException.

So I let ClientControlsImpl implement serializable and I get no exception. But when I call sayHi() the text is printed out at the client!!!!!!! This means that the server has sent the full object and not only a stub. This is clearly not what I want because I want to use this class to communicate with the server...

So I figured that I might need to use the line UnicastRemoteObject.exportObject(c) in the login method before return the

ClientControls object because I have seen this done in an RMI example where they bind a Remote object which isn't a

UnicastRemoteObject.

However when I do this I get the following error at the client:

java.rmi.ServerException: RemoteException occurred in server thread; nested exception is:

java.rmi.StubNotFoundException: Stubclass not found: server.ClientControlsImpl_Stub; nested exception is:

java.lang.ClassNotFoundException: server.ClientControlsImpl_Stub

......

And I thought the rmic wasn't needed after java 1.5?

Can anyone help me solve this very problem that seems so basic...........?

[4005 byte] By [Strider80a] at [2007-11-27 9:48:12]
# 1
It turns out if I replaceUnicastRemoteObject.exportObject(c);byUnicastRemoteObject.exportObject(c, 0);that it works fine! (The second argument is a port number which I believe gets automatically assigned if you put a 0).
Strider80a at 2007-7-13 0:16:26 > top of Java-index,Core,Core APIs...