• Post Reply Bookmark Topic Watch Topic
  • New Topic

Trying to stop a Server  RSS feed

 
Giancarlo Corzo
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

I'm doing an client/server app. using to comunicate them.. sockets...

everything works ok.

So I Build a GUI to Start and stop the server.

But, I don't know how to stop the server. So I try this.

public void run()
{
while (!isFinish()) //A boolean that stop the server
{
try
{
System.out.println("Waiting for connections.");
Socket client = serverSocket.accept();

if (!isFinish())
{
System.out.println("Accepted a connection from:
"+client.getInetAddress());

//This class, process the information send by the client
LocationThread connect = new LocationThread(client);
}
}catch(Exception e)
{
System.out.println(e.getMessage());
e.printStackTrace();
}

}
try
{
System.out.println("Server exiting");
join(); //I don't know if a have to do this.
serverSocket.close();
System.out.println("Server has exit");
}catch(Exception e)
{
System.out.println("Error al cerrar el Socket");
}

to exit from the while, I auto-send myself a conection so a pass the accept part. But I don't know if a had to put a Join to wait for the thread that are still running when I shutdown the server.

Do you have any suggestion, perhaps a diferent way of doing this?.


Thanks.
 
Clivant Yeo
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The first thing that you got to change is your coding. You have to include the while loop inside the first try block. When the server socket is closed, the accept method will throw an exception and hence, your while loop will exit, thereby ending the server.

Regards,
Clivant
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!