Search code examples
javatelnetapache-commons-net

How do I send telnet erase line (EL) with Java Apache commons?


I've been trying to figure this one out for a while now:

I've set up an apache commons telnet client that can send commands to a server and read responses. My program basically works like this:

    TelnetClient telnetClient = new TelnetClient();

    // Add option handlers
    TerminalTypeOptionHandler ttopt = new TerminalTypeOptionHandler(
            "VT100", false, false, true, false);
    EchoOptionHandler echoopt = new EchoOptionHandler(true, false, true,
            false);
    SuppressGAOptionHandler gaopt = new SuppressGAOptionHandler(true, true,
            true, true);
    try {
        telnetClient.addOptionHandler(ttopt);
        telnetClient.addOptionHandler(echoopt);
        telnetClient.addOptionHandler(gaopt);
    } catch (InvalidTelnetOptionException e) {
        System.err.println("Error registering option handlers: "
                + e.getMessage());
        throw new IOException();
    }

    // Connect to server
    telnetClient.connect(SERVER_ADDRESS, SERVER_PORT);

    byte[] data = new byte[] { 'f', 'o', 'o', '\r', '\n' };

    // Send "foo"
    telnetClient.getOutputStream().write(data);
    telnetClient.getOutputStream().flush();

    // ...

    // Erase previous input
    data = new byte[] { (byte) TelnetCommand.IAC, (byte) TelnetCommand.EL,
            '\r', '\n' };

    telnetClient.getOutputStream().write(data);
    telnetClient.getOutputStream().flush();

What i'm trying to do is to send a telnet EL command to erase the entire line of previously sent characters ("foo"). Telnet commands are always sent with a preceding IAC character and that's what i'm sending too.

I have a working implementation of a listener which reads the servers echoes and prints them out.

The problem is that when i send the IAC-EL, the characters are not interpreted as a command. The IAC-EL characters are instead echoed back appended to the previous input as "foo�ø".

Could this be some problem with the line mode set to one character at a time?

Also the server side is runnning VT100 through the telnet. Maybe there is some way to send a VT100 escape sequence to erase the line instead, e.g. "←[1K". I've been unsuccessful with sending a VT100 sequence soo far anyway.


Solution

  • Have you sniffed what is actually sent? I would expect the Apache code to escape the IAC, so you are really sending IAC-IAC-EL.

    You should be calling TelnetClient.sendCommand(EL).