Hello I'm trying to make a connection to a device with openocd as a server and telnet as client connection. When I try to send command through telnet via java programming my Java Gui hangs without giving any error. Someone please help me to understand the procedure for sending command and receiving response from the telnet.
Main code is as follows:
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");
}
}
}
If I try to call halt() command through Gui, it hangs without giving any response.
If I try with command prompt, it gives proper response.
Someone please help me to solve this issue.Thanks in advance.