This week's book giveaway is in the Android forum.
We're giving away four copies of Head First Android and have David & Dawn Griffiths on-line!
See this thread for details.
Win a copy of Head First Android this week in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

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

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
ranger
Posts: 17346
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Mark Spritzler
ranger
Posts: 17346
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 245
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic