File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Sockets and Internet Protocols and the fly likes Shutting down  a server Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "Shutting down  a server" Watch "Shutting down  a server" New topic

Shutting down a server

bhuvana purushoth

Joined: Jun 22, 2001
Posts: 2
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,
Mohamed Yousuff
Ranch Hand

Joined: Jun 23, 2001
Posts: 73

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.
Rajakumar Makapur

Joined: Jun 23, 2001
Posts: 13
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.
Rajakumar Makapur.
I agree. Here's the link:
subject: Shutting down a server
It's not a secret anymore!