javatelnetzmodem

Trying to send a file with zmodem using lrzsz via java telnet server


I am trying to use lrzsz to send a file by zmodem by reading stdio in java and forwarding it to a socket of a connected client.

private void sendFiles(ArrayList<String> filenames) {
    Process p;
    ProcessBuilder pb;

    filenames.add(0, "/opt/local/bin/sz");

    try {

        pb = new ProcessBuilder(filenames);

        p = pb.start();

        InputStreamReader  lrzszin = new InputStreamReader(p.getInputStream());
        OutputStreamWriter lrzszout = new OutputStreamWriter(p.getOutputStream());

        InputStreamReader telnetin = new InputStreamReader(socket.getInputStream());
        OutputStreamWriter telnetout = new OutputStreamWriter(socket.getOutputStream());


        while (p.isAlive()) {
            if (lrzszin.ready()) {
                telnetout.write(lrzszin.read());
                telnetout.flush();
            }
            if (telnetin.ready()) {
                lrzszout.write(telnetin.read());
                lrzszout.flush();
            }
        }


    } catch (IOException ex) {

    } 
}

However the zmodem downloads are coming through corrupted. and it errors out.


Solution

  • Answering my own question...

    I needed to add the correct character encodings to the input and output streams.

            BufferedReader  lrzszin = new BufferedReader(new InputStreamReader(p.getInputStream(), "ISO-8859-1"));
            BufferedWriter lrzszout = new BufferedWriter(new OutputStreamWriter(p.getOutputStream(), "ISO-8859-1"));
    
            BufferedReader telnetin = new BufferedReader(new InputStreamReader(socket.getInputStream(), "ISO-8859-1"));
            BufferedWriter telnetout = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(), "ISO-8859-1"));
    

    While SEXYZ telnet option accounts for the IAC characters, I am not sure if lrzsz does (probably not) so I added Brian's suggestion of escaping them as well.