This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Sockets and Internet Protocols and the fly likes How to close ServerSocket Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "How to close ServerSocket" Watch "How to close ServerSocket" New topic

How to close ServerSocket

MohanRaj Gurubatham

Joined: Sep 05, 2001
Posts: 16
I need to close a server which is running in a console through a client program or in the same program by passing arguments.
I have used close() method of ServerSocket class.. but it is not working. I have given the spurce bellow which I have used.

public void run()
ss= new ServerSocket(config.getServerPort());
int localPort = ss.getLocalPort();
logger.datedLog("\n\n"+NAME+" is listening on port "+localPort+".");
int j=0;


Socket client = ss.accept();
runThread=new XMLInterfaceThread(client,config,logger);

catch(IOException ex)
logger.datedLog("Unable to listen on "+config.getServerPort()+".");

// --------------------
public void stop()

logger.datedLog("Shutingdown XMLInterface ...");
logger.datedLog("XMLInterface is not connected.");

catch(IOException ioe)
logger.datedLog("Unable to close the XMLInterface ."+ioe.getMessage());
// ------------------------
how could I call this stop() method from a client program.
If I create a new instance of the server class it comes into the else part of the stop() method.
solution with source would be appreciated.
Thank you in advance,
netharam ram
Ranch Hand

Joined: Aug 09, 2001
Posts: 202
Hai, Mohan u can do this quiet easily.just create a socket from another program, or the same,sending a message eg."exit".read this & if it is "exit" ,quit the program.
Socket s=new Socket(localhost,config.getServerPort());

then write the message "exit".
close socket s.
Happy middling with java.
Bye from netharam
I agree. Here's the link:
subject: How to close ServerSocket
Similar Threads
restarting and stopping serversocket running on port
Chat At Server Part Both write and read at the same time?
ServerSocket problem w/ Jeode EVM on iPaq
ServerSocket not oppening
Connection is failing at ObjectInputStream for Server