Question

I have a server - client pair written in java and I would like to quit one application if the other one is quit. How is this done? I would also like to know how to make the program do a sequence of commands (e.g. close sockets etc.) if the user quits the application manually (i.e. by pressing cmd+q.alt+F4 etc.)

Was it helpful?

Solution

When one quits, it send a message to the other which causes it to quit.

One approach is the poison pill message/object which cause the receiver to shut down when that message is reached.

OTHER TIPS

Usually the way to do it would be to have a heartbeat thread running separately. The way this works is -

  1. Each client runs a heartbeat thread and will send periodic message to the server essentially saying that "I am running". You can tweak the timing between the message as per your requirement.

  2. The server runs a heartbeat listener thread and will maintain a list of clients running

  3. Once all the clients stop sending message the server can wait a couple of minutes and then gracefully shut down.

This design can work for a single client or many clients.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top