Advertisement
Guest User

Nettogrof

a guest
Dec 2nd, 2009
1,133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.70 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.BufferedWriter;
  3. import java.io.IOException;
  4. import java.io.InputStreamReader;
  5. import java.io.OutputStreamWriter;
  6.  
  7.  
  8. public class TestingCtrlC implements Runnable{
  9.  
  10.    
  11.    
  12.     private Process cmd;
  13.     private BufferedWriter out;
  14.     private BufferedReader input;
  15.  
  16.     public TestingCtrlC(){
  17.     Runtime re = Runtime.getRuntime();
  18.    
  19.    
  20.     try{
  21.        
  22.         cmd = re.exec("ping google.com -n 10000"); // -n is for Windows OS,  for Unix type it's -c  I think
  23.        
  24.        
  25.         out = new BufferedWriter (new
  26.                   OutputStreamWriter(cmd.getOutputStream()));
  27.         input =  new BufferedReader (new
  28.                   InputStreamReader(cmd.getInputStream()));
  29.        
  30.         new Thread(this).start();
  31.        
  32.         try{
  33.             Thread.sleep(5000);
  34.         }catch(InterruptedException ie){
  35.             ie.printStackTrace();
  36.         }
  37.        
  38.        
  39.         System.out.println("***** Sending ctrl-c   break code***");
  40.         char ctrlBreak = (char)3;
  41.                
  42.        
  43.         //Different testing way to send the ctrlBreak;
  44.         out.write(ctrlBreak);
  45.         out.flush();
  46.         out.newLine();
  47.         out.flush();
  48.  
  49.         out.append(ctrlBreak);
  50.         out.flush();
  51.        
  52.         out.append(ctrlBreak);
  53.         out.newLine();
  54.        
  55.         out.flush();
  56.        
  57.         out.write(ctrlBreak+"\n");
  58.         out.flush();
  59.        
  60.         System.out.println("***** No more ctrl-c   break code***");
  61.     }catch (IOException ioe){
  62.        
  63.         ioe.printStackTrace();
  64.        
  65.     }
  66.     }
  67.    
  68.  
  69.     @Override
  70.     public void run() {
  71.     String line;
  72.     try{
  73.             while((line = input.readLine()) != null){
  74.                 System.out.println(line);
  75.             }
  76.     }catch(IOException ioe){
  77.         ioe.printStackTrace();
  78.     }
  79.    
  80.     }
  81.    
  82.    
  83.     /**
  84.      * @param args
  85.      */
  86.     public static void main(String[] args) {
  87.    
  88.     new TestingCtrlC();
  89.     }
  90.  
  91. }
  92.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement