无法建立与主机的Telnet连接

时间:2019-02-18 10:42:49

标签: java telnet urlconnection

当前,我正在通过端口23上的特定主机建立telnet连接。我在Internet上看到JTelnet源代码,并尝试连接到本地网络主机。但是我无法建立连接。我尝试连接的主机中甚至没有防火墙阻止。

那么你们能给我关于如何解决此问题的任何想法吗?

请在下面找到Java代码:

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();
        }


    }

} 

请在堆栈跟踪下面找到

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)

0 个答案:

没有答案