import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintStream;
import org.apache.commons.net.telnet.EchoOptionHandler;
import org.apache.commons.net.telnet.TelnetClient;
import org.apache.commons.net.telnet.TerminalTypeOptionHandler;
import org.apache.commons.net.telnet.WindowSizeOptionHandler;
public class TelnetClientTest {
public static void main(String[] args) throws Exception {
TelnetClient tc = new TelnetClient();
tc.addOptionHandler(new TerminalTypeOptionHandler("VT100", false, false, true, false));
tc.addOptionHandler(new EchoOptionHandler(true, true, true, true));
tc.addOptionHandler(new WindowSizeOptionHandler(80, 25, true, false, true, false));
String ip = "xxx.xxx.xx.x";
int port = 23;
tc.connect(ip, port);
InputStream in = tc.getInputStream();
OutputStream out = tc.getOutputStream();
Thread t1 = new Thread() {
@Override
public void run() {
try {
byte[] buff = new byte[10240];
int len = 0;
while ((len = in.read(buff, 0, buff.length)) != -1) {
System.out.print(new String(buff, 0, len));
}
System.out.println("EXIT");
System.exit(0);
} catch (Exception e) {
e.printStackTrace();
}
}
};
t1.start();
PrintStream ps = new PrintStream(out);
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String line = null;
while ((line = br.readLine()) != null) {
ps.println(line);
ps.flush();
}
br.close();
}
}