通过telnet发送命令时Gui挂起(Java编程)

时间:2019-03-14 07:03:24

标签: java client-server telnet telnetlib openocd

您好,我正在尝试使用openocd作为服务器并使用telnet作为客户端连接来建立与设备的连接。当我尝试通过Java编程通过telnet发送命令时,Java Gui挂起,没有给出任何错误。请有人帮助我了解发送命令和接收telnet响应的过程。

主要代码如下:

 public class JTagMain 
{
    static TelnetClient telnet;
    public static void halt() {
        telnet = new TelnetClient();
        try {
            telnet.connect("localhost", 4444);
            String cmd = "halt";
            telnet.getOutputStream().write(cmd.getBytes());
        } catch (IOException e) {
            e.printStackTrace();
            System.exit(1);
        }
        readWrite(telnet.getInputStream(), telnet.getOutputStream(),
                System.in, System.out);
        try {
            telnet.disconnect();
        } catch (IOException e) {
            e.printStackTrace();
            System.exit(1);
        }
        System.exit(0);
    }
    public static final void readWrite(final InputStream remoteInput,final OutputStream remoteOutput, final InputStream localInput,
            final OutputStream localOutput)
    {
        System.out.println("-----readwrite---");
        Thread reader, writer;
        reader = new Thread()
        {
            public void run()
            {
                int ch;
                try
                {
                    while (!interrupted() && (ch = localInput.read()) != -1)
                    {
                        remoteOutput.write(ch);
                        remoteOutput.flush();
                    }
                }
                catch (IOException e)
                {
                    //e.printStackTrace();
                }
            }
        };
        writer = new Thread()
        {
            public void run()
            {
                try
                {
                    Util.copyStream(remoteInput, localOutput);
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                    System.exit(1);
                }
            }
        };

        writer.setPriority(Thread.currentThread().getPriority() + 1);
        writer.start();
        reader.setDaemon(true);
        reader.start();
        try
        {
            writer.join();
            reader.interrupt();
        }
        catch (InterruptedException e)
        {
            //e.printStackTrace();
        }
    }

    public static void telnetconnection()
    {
        telnet = new TelnetClient();
        try {
            telnet.connect("localhost", 4444);
            System.out.println("telnetconnection");
        } catch (IOException e) {
            e.printStackTrace();
            System.exit(1);
        }
    }
    public static void main(String[] args) 
    {
        JtagGui window = new JtagGui();
        window.frame.setVisible(true);
        try{
            telnetconnection();
        }
        catch(Exception e){
            System.out.println("connection failed");
        }
    }
}

如果我尝试通过Gui调用halt()命令,它将挂起而未给出任何响应。

如果我尝试使用命令提示符,它将给出正确的响应。

请有人帮助我解决此问题。谢谢。

0 个答案:

没有答案