?
Solved

Stopping a Programme

Posted on 2005-03-05
6
Medium Priority
?
235 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 

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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
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…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses
Course of the Month13 days, 5 hours left to enroll

777 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