This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
hi all, First let me give you an idea of the basic framework of my program. Then I'll let you know what I am having trouble with. I have a GUI with a button, this button both starts and stops a server. It starts the server by creating the server object, then it creates a thread to use the server. So, this thread accesses the server.waitForConnection() method that uses the server socket to accept incoming connections. When a new connection is established, another thread is spawned to handle the connection and it goes back to accepting... (I hope that is clear) What I want: I want the user to be able to stop the server. Doing so cleans up everything and destroys the server object. The user can then start the server again and a new clean server is created. What my problem is: How do I clean up all the client connections and threads for every client that connects (there can be any number of these), clean the server sockets and the thread running in the GUI, then clean up the reference to the server socket? I am not familiar with Java garbage collection etc. So please any help would be greatly appreciated. Thank you, Bhuvana..
Use the setSoTimeout() method of the ServerSocket object before you call the accept method. This will throw an exception when there is no connection request within a specified time. At this time you can check if the stop button was pressed. If so then you can clean up your program and close the serversocket. Also when any new connection is accepted, the socket shall be put in a java.util.Vector so that all that can be retrieved and closed when you perform a shutdown operation. Hope you understand the solution. Good Luck.
Hi You try with ThreadGroup class. So that while shutting down the server, you take all threads in enumeration and stop all threads. check out ThreadGroup class for more details. regards Rajakumar Makapur.