• 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:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

How to close ServerSocket

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
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()
{
try
{
ss= new ServerSocket(config.getServerPort());
int localPort = ss.getLocalPort();
logger.datedLog("\n\n"+NAME+" is listening on port "+localPort+".");
int j=0;

while(!stopRequested)
{

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

}
catch(IOException ex)
{
logger.datedLog("Unable to listen on "+config.getServerPort()+".");
System.exit(0);
}

}
// --------------------
public void stop()
{
stopRequested=true;
try
{
if(ss!=null)

{
logger.datedLog("Shutingdown XMLInterface ...");
runThread.interrupt();
ss.close();
}
else
{
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,
GMohanraj
 
Ranch Hand
Posts: 202
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
eg.
Socket s=new Socket(localhost,config.getServerPort());

then write the message "exit".
close socket s.
Happy middling with java.
Bye from netharam
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic