javarmiremoteexception

Java RMI RemoteException after sending last message


So I've made a client/server pair in Java using RMI. The point is to send messages from the client to the server and print out how many messages we've received and how many we've lost (im comparing it to UDP so dont ask why I'm expecting to lose any with RMI).

So anyway, written the code, everything seems to work fine, I bind to the server and send the messages, the server receives them all and outputs the results.

Only problem is, after I've sent the last message from the client it throws a RemoteException and I have no idea why.

My only guess is that the server has shut down fist so its letting me know I can't contact the server any more (ie my iRMIServer variable is now invalid).

Funny thing is I thought that the client would shut down first because it terminates after sending the messages. The reason it might not be shutting down first is because it has to wait for a reply (ACK) from the server to confirm receipt of the message??

Maybe in this overlap time of the server replying to let us know everything is ok, the server shuts down and we can't connect to it again.

The code for sending the messages at the client end is as follows:

   try { 
         iRMIServer = (RMIServerI) Naming.lookup(urlServer); 

         // Attempt to send messages the specified number of times 
         for(int i = 0; i < numMessages; i++) { 
            MessageInfo msg = new MessageInfo(numMessages,i); 
            iRMIServer.receiveMessage(msg); 
         } 
    } catch (MalformedURLException e) { 
        System.out.println("Errpr: Malformed hostname."); 
    } catch (RemoteException e) { 
        System.out.println("Error: Remote Exception."); 
    } catch (NotBoundException e) { 
        System.out.println("Error: Not Bound Exception."); 
    } 

So it is sending the messages from 0-999 if I select 1000 messages to be sent.

After printing out the results from the server I call System.exit() straight away which could cause it to terminate early without waiting for the appropriate responses from the client?

If you can help I'd be greatly appreciative, and if you need any more info I'd be happy to provide.

Thanks in advance.


Solution

  • You can't shutdown the server in the middle of a remote method. The server has to send back an OK or exception status, or a return value if the method has one, and that's what your client is failing on when trying to receive. You have to schedule the shutdown to execute a bit later.