You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by di...@pansoft.de on 2008/09/23 22:48:14 UTC
[net] commons net TelnetClient inputstream blocks too long
Hi,
I've got an telnet enabled device of wich I could read the actual
temperature and humidness. If I connect to this device via telnet
everything works as it should. Following you could see an example telnet
session:
output of telnet connection:
$ telnet
telnet> toggle prettydump
Will print user readable output for "netdata".
telnet> open 192.168.127.254 4002
Trying 192.168.127.254...
Connected to 192.168.127.254.
Escape character is '^]'.
{x00A}
{ $x00AJB
{x00V}
{ $x00V1.0\
{x00D}
{ $x00DT071077F123032201I
{x00M}
{ $x00MT027.3F032.5P
So I wrote a little Test Application doing the (as I think) same via the
commons net API. Output of my Application is as follows:
Consoleoutput of java program:
server alive:true
Requesting {x00A}
Response: {
Requesting {x00V}
Response: $x00AJB
Requesting {x00D}
Response: { $x00V1.0\
Requesting {x00M}
Response: { $x00DT067077F168032242\
After the first command I got an incomplete response. After sending the
second command I get the remaining of what should be the first response,
after the third command I get the response of my second request and so
on... Finally I don't get the last response at all, as long as I try to
read.
Here is the source of my java Application:
package de.test;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.SocketException;
import org.apache.commons.net.telnet.TelnetClient;
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
TelnetClient tc = new TelnetClient();
try {
tc.connect("192.168.127.254", 4002);
} catch (SocketException e) {
e.printStackTrace();
System.exit(1);
} catch (IOException e) {
e.printStackTrace();
System.exit(1);
}
InputStream instream = tc.getInputStream();
OutputStream outstream = tc.getOutputStream();
try {
try {
boolean alive = tc.sendAYT(10000);
System.out.println("server alive:" + alive);
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
// be sure no data awaits reading before sending first command
readResponse(instream);
outstream.write("{x00A}\r\n".getBytes());
outstream.flush();
System.out.println("Requesting {x00A}");
String response = readResponse(instream);
System.out.println("Response:\t" + response);
System.out.println("Requesting {x00V}");
outstream.write("{x00V}\r\n".getBytes());
outstream.flush();
response = readResponse(instream);
System.out.println("Response:\t" + response);
System.out.println("Requesting {x00D}");
outstream.write("{x00D}\r\n".getBytes());
outstream.flush();
response = readResponse(instream);
System.out.println("Response:\t" + response);
System.out.println("Requesting {x00M}");
outstream.write("{x00M}\r\n".getBytes());
outstream.flush();
response = readResponse(instream);
System.out.println("Response:\t" + response);
} catch (IOException e) {
e.printStackTrace();
} finally
{
try {
tc.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}
}
private static String readResponse(InputStream instream) throws
IOException {
StringBuilder result = new StringBuilder();
while (instream.available() == 0)
{
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
byte[] buffer = new byte[1024];
while (instream.available() > 0)
{
int count = instream.read(buffer);
result.append(new String(buffer, 0, count));
}
return result.toString();
}
}
Am I doing something wrong or is there an Bug in the stream
implementation of the TelnetClient's inputStream?
By the way, if I run the TelnetClientExample of the commons net package,
the effect is the same as with my java program. After typing the first
command the response is only an '{'. After the second command the
remaining of the response gets written to System.out and so on.
thanks for your Help in advance,
Dirk Kaspar
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org