I am trying to connect to AS400 using the TelnetClient from Apache in Java. The goal for now is to simply connect and read the screen line by line and display each line. I will focus on input and interaction later.
When I connect via telnet, my program gets 'hung up' when it tries to read the input line. I think the thread is blocked by: line = bfIn.readLine()
because this is where the application stops. It does not crash, it is just stuck there.
If I connect to a simple telnet switch using a different host and the same program, the program prints out the page just fine, so I am wondering if there is an extra step to connect to the AS400 specifically? Can you connect to AS400 through Apache TelnetClient for java? If not, how can you connect to AS400 through java and interact with the page (seeing what the page displays)?
Here is the code I have been working with:
import org.apache.commons.net.telnet.TelnetClient;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.io.OutputStreamWriter;
public class AutomatedTelnetClient {
private TelnetClient telnet = new TelnetClient();
private InputStream in;
private PrintStream out;
private String prompt = "%";
public AutomatedTelnetClient(String server, String user, String password) {
try {
// Connect to the specified server
telnet.connect(host, 23);
in = telnet.getInputStream();
out = new PrintStream(telnet.getOutputStream());
BufferedReader bfIn = new BufferedReader(new InputStreamReader(in));
PrintWriter writer = new PrintWriter(telnet.getOutputStream(), true);
System.out.println("BufferedReader ready to be read: " + bfIn.ready());
try {
String line;
while ((line = bfIn.readLine()) != null) {
System.out.println(line);
}
}
catch (IOException e) {
System.out.println("Exception");
e.printStackTrace();
}
telnet.disconnect();
in.close();
bfIn.close();
An issue which I think might be occurring is the BufferedReader may not be able to pick up what AS400 returns to it upon connection so it cannot read it correctly.
Please note that the bufferedReader.ready() is returning false. I have made sure the host is valid and working using a command line connection.