aspose file tools*
The moose likes Beginning Java and the fly likes Can't catch Window Close event when trying SocketServer trying to accept connections Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Can Watch "Can New topic
Author

Can't catch Window Close event when trying SocketServer trying to accept connections

Grant Baxter
Greenhorn

Joined: May 13, 2005
Posts: 4
Hi All,

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?

Do I go into such a tight loop here:

that I can't handle the WindowListener event?





[ November 09, 2005: Message edited by: Grant Baxter ]
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17259
    
    6

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.

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17259
    
    6

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.

Mark
Vlado Zajac
Ranch Hand

Joined: Aug 03, 2004
Posts: 245
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 ]
 
 
subject: Can't catch Window Close event when trying SocketServer trying to accept connections