问题
Currently, I'm working on a chat server-client program which is built on Java RMI. In order for a client to detect that its connected server has been died, I can only think of two possible solutions, but neither of them works well in my case.
1. Wait until the client needs to invoke some methods of the server, this way does not work well because if the chat client does not invoke any method of server, it will not know that server is dead, thus the chat client can not receive any new chat message in that time anymore.
2. Routine check by invoking a server's method every few seconds. But this introduces a large amount of overhead.
Is there any better way for this case?
Thank you very much.
回答1:
So nobody seems to be answering this ... How would you know if something is dead without interacting with it?
In IRC the server will periodically ping clients to check that they're still connected ...You will see Ping! Pong!
in the client window and sometimes a client will attempt to send a message only to find that it has no connection.
If you are worried about overhead .. just don't do it so often
来源:https://stackoverflow.com/questions/19274163/in-java-rmi-how-can-a-client-know-that-its-server-is-dead