0

Currently I am working on setting up a telnet connection via a particular host on port 23. I saw JTelnet source code in the internet and tried connecting to my local network host. But I am not able to establish a connection. There is not even firewall block in the host with which I am trying to connect.

So Can you guys give me any idea on how to resolve this issue?

Please find below the java code:

BasicTelnet.java

public class BasicTelnet extends Canvas implements Runnable {
    final static boolean debug = false;
    final static int columns = 80, lines = 24;
    final static int telnetPort = 23;
    int PTWidth, PTHeight, charOffset, lineOffset, topOffset;
    char screen[][];
    Color screenfg[][];
    Color screenbg[][];
    boolean redraw = true;
    boolean lineRedraw[];
    Color bgcolor = Color.white;
    Color fgcolor = Color.black;
    Color d_bgcolor = Color.white;
    Color d_fgcolor = Color.black;
    Font PTFont;
    //
    boolean wrap = true;
    int xloc = 0, yloc = 0;
    InputStream sIn;
    OutputStream sOut;
    Thread receive;
    int telnetState = 0;
    boolean option[];
    DefaultTelnetTerminalHandler telnetHandler;
    Image backBuffer;

    {
        telnetHandler = new BasicTelnetTerminalHandler();
    }

    class BasicTelnetTerminalHandler extends DefaultTelnetTerminalHandler {
        public void LineFeed() {
            yloc++;
            xloc = 0;
            scrValid();
            lineRedraw[yloc] = true;
        }

        public void BackSpace() {
            xloc--;
            if (xloc < 0) {
                yloc--;
                xloc = columns - 1;
                if (yloc < 0)
                    yloc = 0;
            }
        }

        public void HorizontalTab() {
            int n = (8 - xloc % 8);
            for (int j = 0; j < n; j++)
                normal((byte) 32);
        }

        public void CarriageReturn() {
        }

        public void Null() {
        }

        public void FormFeed() {
        }

        public void ClearScreen() {
        }

        public void VerticalTab() {
        }
    }

    public static void main(String[] args) {
        try {
            String host = (args.length > 0) ? args[0] : "graphics.nyu.edu";
            int port = (args.length > 1) ? Integer.parseInt(args[1]) : telnetPort;
            Component t = new BasicTelnet(host, port);
            Frame f = new Frame(host) {
                public void update(Graphics g) {
                }

                public void paint(Graphics g) {
                }
            };
            f.setLayout(new BorderLayout());
            f.add(t, BorderLayout.CENTER);
            f.setResizable(false);
            f.pack();
            f.show();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * openConnection() uses thor.net.URLStreamHandler to open a
     * "telnet://host:port" connection.
     */
    void openConnection(String host, int port) throws IOException {
//      host = "10.30.65.9";
        URL url = new URL("telnet", host, port, "", new net.URLStreamHandler());
        URLConnection urlConnection = url.openConnection();
        urlConnection.connect();
        ((TelnetURLConnection) urlConnection).setTelnetTerminalHandler(telnetHandler);
        sOut = urlConnection.getOutputStream();
        sIn = urlConnection.getInputStream();

        telnetHandler.addWantedOption(1); // char by char mode
        ((TelnetURLConnection) urlConnection).updateOptions();

        receive = new Thread(this);
        receive.start();
    }


}

TestTelnetConnection.java

public class TestTelnetConnection {
    public static void main(String[] args) {

        String host2 = "10.30.65.9";
        int port = 23;
        try {
    BasicTelnet bTelnet = new BasicTelnet(host2);

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


    }

} 

Please find below the stack trace

Exception in thread "main" java.lang.NullPointerException
    at telnet.client.BasicTelnet.openConnection(BasicTelnet.java:149)
    at telnet.client.BasicTelnet.<init>(BasicTelnet.java:273)
    at telnet.client.BasicTelnet.<init>(BasicTelnet.java:248)
    at telnet.client.TestTelnetConnection.main(TestTelnetConnection.java:11)
ashwini
  • 35
  • 1
  • 8
  • The stack trace tells you *what* is wrong (a null pointer exception, that is you attempt to use an object which is null), and where it happens (`telnet.client.BasicTelnet.openConnection(BasicTelnet.java:149)`). From that information and possibly a little [debugging](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) it shouldn't be to hard to figure out what the problem might be and hopefully how to solve it. – Some programmer dude Feb 18 '19 at 10:46
  • And if you want our help, then please try to create a [**Minimal**, Complete, and Verifiable Example](https://stackoverflow.com/help/mcve) to show us. And please read about [how to ask good questions](http://stackoverflow.com/help/how-to-ask), as well as [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). – Some programmer dude Feb 18 '19 at 10:47
  • The null pointer exception occured because its not able to connect to the host. I don't know why its not able to connect when I run the code. – ashwini Feb 18 '19 at 10:48
  • What code is in line 149? – f1sh Feb 18 '19 at 10:50
  • @f1sh This is the code in the line 149 "urlConnection.connect();" – ashwini Feb 18 '19 at 10:51
  • 2
    Perhaps you should add a check for a null `urlConnection`? And attempt to get some information about why the connection failed. And of course, take a look at the server-side to make sure it's all working there (and that you actually *can* connect to the remote host!). – Some programmer dude Feb 18 '19 at 10:53
  • Possible duplicate of [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Antoniossss Feb 18 '19 at 10:53

0 Answers0