Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Stopping a Programme

Posted on 2005-03-05
6
Medium Priority
?
236 Views
Last Modified: 2010-03-31
Hi

My programme in java is running a while loop waiting for socket connections. However I need to also have something to close the programme safely, as it runs from a command line (the programe is a server)

Using in.readline() from input from the command line means the while loop stops at that code before doing anything else.

How else can I have safely shutdown the programme using the above way? without it stopping and waiting for input.

Thanks in advance

Akbar
0
Comment
Question by:akzah
6 Comments
 
LVL 5

Expert Comment

by:ashok3sep
ID: 13468735
System.exit(0)
0
 
LVL 2

Expert Comment

by:C0d3dM1nd
ID: 13469202
Runtime.getRuntime().exit(0);
or
like ashok3sep said: System.exit(0);

C0d3d M1nd
0
 

Author Comment

by:akzah
ID: 13470287
I am already using that. Its how to get round the in.readline()  in the while loop??

As it stops there waiting for input each time, I want it to ignore in.readline() unless there is actually an input??
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Expert Comment

by:scorpio05678
ID: 13470367
You could listen for keyboard events without readline in the while loop.

Use awt event and in the action performed method check what key is pressed (if any)  and then decide if you want to exit the program otherwise carry on as normal.

hope it helps

SC

0
 
LVL 86

Accepted Solution

by:
CEHJ earned 200 total points
ID: 13471010
You need to close the ServerSocket in another thread:


import java.io.*;
import java.net.*;

public class SocketStop {
      boolean keepReading = true;
      ServerSocket ss;


      public static void main(String[] args) throws Exception {
            SocketStop sStop = new SocketStop();
            sStop.readExitSignal();
            sStop.loopRead();
      }

      private void readExitSignal() {
            new Thread(new ExitSignalReader()).start();
      }

      class ExitSignalReader implements Runnable {
            public void run() {
                  try {
                        System.out.print("Enter 'exit' to stop: ");
                        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
                        if ("exit".equals(in.readLine())) {
                              keepReading = false;
                              ss.close();
                        }
                  }
                  catch(IOException e) {
                        e.printStackTrace();
                  }
            }
      }

      private void loopRead() {
            try {
                  ss = new ServerSocket(5555);
                  while (keepReading) {
                        Socket s = ss.accept();
                  }
            }
            catch(IOException e) {
                  if (keepReading) {
                        e.printStackTrace();
                  }
            }
      }


}
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 13475408
:-)
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses
Course of the Month12 days, 3 hours left to enroll

564 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question