Place all six .java files together with the client.policy file in the same folder. From the command line run the following commands: >start rmiregistry >start java TestServer >start java TestClient //client1 >start java TestClient //client2 Push dialog box OK button to close client1. Push dialog box OK button to close client2.
This works fine.
However, if I close client2 before client1 I get java.rmi.ServerException etc.
This only seems to be a problem when the clients have the same name, so I guess each client should be identified by something else than their name which is not necessary unique.
PS: I have been going back and forth for far too long on this little problem already and in the process I may have overlooked some (basic) cause for this problem to appear. Any help will be appreciated.
When each client registers they are put into the list in order... when you go to take them out, you search by name, which goes in order. So, if you call unregister() from client 2 first, client 1 is really removed from the list on the server, since they both have the same name. Client 2 then exits. Client 1 is active, and calls unregister(). Inside unregister() the server loops through it's list, checking names. The only reference in the list now is to client 2. The reference is still there in the server, *but client 2 is no longer running at this point*. In this section of the code -
the method call to client.getName() is really a remote call to the referenced client. This reference is to client 2, a client that doesn't exist any more, so there's probably a SocketException thrown back to the server. The server probably wraps this in a ServerException and throws it back to client 1.
There are a couple of ways to fix this. One is to just add code to register() to make sure that only one client gets to use the same name. Another would be to have the register method create some type of unique identifier (i.e. timestamp and some random number) for the client and make it a return value of the register() method, and have the client saved under this unique identifier. Then have the client pass the unique identifer back to unregister() so you can make sure you're removing the right client.
Write once, run anywhere, because there's nowhere to hide! - /. A.C.