3

In perl, we can do this:

print("\x03");

to do Ctrl+C

Is there something similar we can do in Java?

tcyrus
  • 50
  • 6
rv1822
  • 169
  • 3
  • 10
  • 1
    Refer this **[http://stackoverflow.com/questions/1216172/java-how-could-i-intercept-ctrlc-in-a-cli-application](http://stackoverflow.com/questions/1216172/java-how-could-i-intercept-ctrlc-in-a-cli-application)** and **[http://stackoverflow.com/questions/1611931/catching-ctrlc-in-java](http://stackoverflow.com/questions/1611931/catching-ctrlc-in-java)** – Siva Charan Feb 25 '12 at 07:51
  • If the other end is a telnet session, why don't you simply close the session? – tripleee Feb 25 '12 at 10:33
  • Because I need to use the connection further. And disconnecting and connecting again is not working. – rv1822 Feb 25 '12 at 11:16

2 Answers2

7

In perl, we can do this : [...]

Really? Did you try and it terminated any program?

This example

#!/usr/bin/perl
print "\x03";
print "\n";
print "Hello World!\n";

Will print

<funny character>
Hello World!

Piping the output to some other program will also not terminate any of them.

You want to send the ^C signal. But you really just send the 0x03 byte. If you want to terminate someone you have to use something like this (OS dependend):

kill(process_id_of_destination, SIGTERM);

The confusion's source is, that in a normal terminal window the OS intercepts the ^C key and translates it into the TERM signal. But the program will not receive that ^C on its input stream, it will receive the signal and it might process it in some registered hook. So if you write ^C yourself it is just plain data and will not be processed specially.

Addendum

The following text applies to Unixoids/Linux only, but most probably not to Windows. I think that's OK, because telnet, perl, tail -f indicate Unix background.

As already mentioned: sending ^C (aka. the byte 0x03) to the standard input of a process will not trigger any signal. Usually. But the terminal window itself and some other programs like rlogin, telnet, sshd must pretend, that the stuff running within then is connected directly to a terminal. This includes the transformation of some key combos into some signals. This is done using "pseudo terminals" (aka. pty or pts). For details on this stuff lookup man 4 pts and man 7 pty and/or read the Unix book of Richard Stevens. The excerpt for your case is: Data written to the "master PTY" will be handled exactly like keyboard input. So a ^C written into the master will be translated into a signal which is delivered to the process reading the "slave PTY". So if your print("\x03"); in Perl works and the Java version System.out.println('\3'); doesn't, then I assume that Perl is somehow set up differently and writes to a PTY master and Java writes only to a plain pipe.

A.H.
  • 63,967
  • 15
  • 92
  • 126
  • here's what i'm trying to do - `$telnet->print("tail -f logfile.log");//waitfor some character and then $telnet->print("\x03");`. This works. I was trying to do the same in java but i'm not able to get back to prompt. – rv1822 Feb 25 '12 at 10:16
  • It's not a "command", it's a signal (the terminology later in your answer is correct). – tripleee Feb 25 '12 at 10:31
  • Actually, SIGTERM followed by SIGKILL if needed :) – DVK Feb 25 '12 at 13:05
  • @rv1822: How is the `$telnet` thing initialized? Is it a PTY? Or is it a plain pipe? A FIFO? – A.H. Feb 25 '12 at 17:33
  • @tripleee: The first version of the question was titled with "...command..." so I tough this term might be clearer to the QO. But you are right. – A.H. Feb 25 '12 at 17:38
-2

Have you tried:

System.out.print((char)3);
Jon Lin
  • 142,182
  • 29
  • 220
  • 220