Can't catch Window Close event when trying SocketServer trying to accept connections
Joined: May 13, 2005
I'm working on a simple server that accepts incoming connections and echoes incoming text back to client. I've built a gui w/ a JFrame so that I can eventually add some interaction w/ the clients. My main server class extends JFrame.
Here's my dilemma -- I have two methods that I call in the constructor of my server class: initGUI() and startServer(). I've added a WindowListener to my JFrame so that I can shut down gracefully when the window is closed. When startServer() is commented out and I click the "X" on the JFrame, I catch the window event and can go to my shutdown routine. However, as soon as I uncomment the startServer() routine, when I click the "X" on the JFrame, it disappears, but the program keeps running. I can continue to connect to the server, so it's happily running.
Why do I miss the WindowListener event when startServer() is executed and miss it when it's not?
Probably, and I didn't look at most of your code, but the big money is on the fact that you have only closed the JFrame, not the program itself, so there is still a reference to a running Server, which is probably a Daemon thread continually running until you stop it. You can have a shutdown method in the Server code that sets a boolean variable to true or false, and in the loop that sets up the Daemon thread have it check for that variable and exit the loop and shut down the thread.
Then in the main program set your reference to null.
OH, and I am also assuming that the Server is running on a seperate thread, otherwise your sever is using the thread called main, the only thread running, so it is probably blocking if it does not have a seperate thread.
Joined: Aug 03, 2004
from BPMServer constructor:
startServer() contains infinite loop so it never ends. addWindowListener is not executed.
startServer(port) should be last call in the constructor or be outside of the constructor (at the end of main method).
Posted code also contains 2 compile time errors which I had to fix (comment out) before running the program. [ November 10, 2005: Message edited by: Vlado Zajac ]