I have a device which implements a Telnet server and I am writing java code to communicate with it. The current implementation "works" and by that I mean it will for a while then hangs, sometimes immediately, sometimes after an hour or anywhere in between. I am not ruling out the possibility that it is the device itself, however, long running sessions with putty seem to have no trouble, so I am working under the assumption that it is my java code.
I'll lay out my code first then describe the problem in more detail:
The connection is handled by an object, EthernetTelnetConnection()
which uses Apache Commons TelnetClient()
under the hood.
public EthernetTelnetConnection(String host, int port) throws IOException {
mTelnet = new TelnetClient();
mTelnet.connect(host, port);
mOut = mTelnet.getOutputStream();
mIn = mTelnet.getInputStream();
}
These streams are converted into buffered streams via:
mReadStream = new BufferedInputStream(mConnection.getInputStream());
mWriteStream = new BufferedOutputStream(mConnection.getOutputStream());
And can be acquired via:
public BufferedInputStream getInputStream() {
return mReadStream;
}
public BufferedOutputStream getOutputStream() {
return mWriteStream;
}
So after creating the connection, I start trying to parse the stream. This occurs in a dedicated thread who's only purpose in life is to execute this one method:
public void detectMessages() throws IOException {
final BufferedInputStream in = mTekdaqc.getInputStream();
StringBuilder builder = new StringBuilder();
int data;
final Scanner scan = new Scanner(in, "UTF-8").useDelimiter(Character.toString((char) AASCIIMessage.RECORD_SEPARATOR_CHAR));
while (scan.hasNext()) {
onMessageDetected(scan.next());
}
/*while ((data = in.read()) != -1) {
if (data == AASCIIMessage.RECORD_SEPARATOR_CHAR) {
onMessageDetected(builder.toString());
builder = new StringBuilder();
} else {
builder.append((char) data);
}
}*/
}
And AASCIIMessage.RECORD_SEPARATOR_CHAR
is defined as:
public static final int RECORD_SEPARATOR_CHAR = 0x1E;
There are some extra variables there from the commented section at the bottom which was my previous attempt. BOTH METHODS EXHIBIT THE SAME PROBLEM which is: onMessageDetected()
eventually gets called with tens of thousands of strings which are empty. Usually if I let things run, eventually things continue as usual for a while then this problem begins again. So I am trying to figure out what is causing both the scanner and the simple while loop/read cycle to detect message delimiters. When capturing the wireshark traffic between the device and putty I can confirm that it is not sending these separators, though it is sending keep alive packets.
UPDATE
It was suggested to me that this might be a problem where the Apache TelnetClient is interpreting a sequence of bytes as a terminal command which Putty is not. I am not sure what terminal implementation the device implements as I did not create that code and it is not documented. I took a look at putty and it does not indicate a terminal type in the Telnet configuration window.
UPDATE 2
I hooked up System.out
to the spy stream of the TelnetClient
instance and examine the output in a hex editor and discovered that the stream is actually seeing repeating RS (0x1E)
characters. Sniffing the traffic with a Putty Telnet connection I do not see this, so is there some sort of configuration that is incorrect with the TelnetClient
class and my device?