0

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.

Olaf Kock
  • 46,930
  • 8
  • 59
  • 90
ghDev
  • 170
  • 1
  • 12
  • 1
    Does it work with the native telnet client of Windows/Linux? – Robert Nov 21 '19 at 18:12
  • Do you mean if the host works on the native telnet client? If so, yes it does. – ghDev Nov 21 '19 at 19:25
  • 1
    Have you tried the TelnetClientExample that's part of the Apache commons library? I just tried building and running that and it seemed to work fine on version 7.4. That will help you determine if its something in your code or if its an issue with the system you are connecting to. It does not use BufferedReader though. – Player1st Nov 21 '19 at 22:55
  • I tried the TelnetClientExample and got a response back. However the response contained characters that were mostly not correct. By not correct I mean the chars seemed like they were not ASCII (little question marks in a box or weird question marks). Here is an example (note inside the boxes would be little question marks): [?3l[?7h[6;53H[?3l[?7h[1;1H[2J[0m [1;22H[1m[0m [1m – ghDev Nov 22 '19 at 13:12
  • Update: I removed the ANSI control characters and seem to have a better quality output. – ghDev Nov 22 '19 at 14:02

1 Answers1

0

Standard telnet can work with the IBM i, but it's not ideal since the 5250 protocol is designed for "screen at a time" "smart" terminals, not line or character at a time dumb terminals.

Are you aware of the TN5250J project?

tn5250j is a 5250 terminal emulator for the AS/400 written in Java.

Also for mobile access, something else to look at would be IBM i Mobile Access provided by IBM.

Charles
  • 21,637
  • 1
  • 20
  • 44
  • I figured there was something with a "smart" terminal. No I haven't heard of that. But unfortunately I don't need a terminal emulator. I need to access the pages in Java so I can display it on an Android device. I believe this emulator would only help for a desktop interface right? Is there a way to use a character at a time or line by line approach through Java? – ghDev Nov 22 '19 at 12:57
  • Anything that display's a 5250 screen would be a called a *terminal emulator" https://play.google.com/store/apps/details?id=dk.mochsoft.tn5250&hl=en_US – Charles Nov 22 '19 at 15:03
  • Yes something like Mocha TN5250 is good, but I would essentially need to embed that functionality into my application if that makes sense. I do need the functionality that they would provide, but I am trying to create a simpler version of that inside my application. – ghDev Nov 22 '19 at 15:19
  • I'd recommend starting with TN5250J...note I updated the link to the latest version of the site – Charles Nov 22 '19 at 15:50