I've been trying to useorg.apache.commons.net.telnet.TelnetClient
and I am having trouble to receive non-ASCII characters (in my case polish chars like ą,ę,ć,ź and few others). The problem is not on server side - when I use default Ubuntu telnet implementation or Putty I have no problem receiving non-ASCII characters. My code looks something like this (simplified a bit for readability):
TelnetClient telnetClient = new TelnetClient();
telnetClient.connect("169.254.24.223", 23);
while (true) {
int readCharInt = telnetClient.getInputStream().read();
if (readCharInt != -1) {
String s = String.valueOf((char) readCharInt);
System.out.print(s);
} else {
System.out.println("EOS");
break;
}
}
I used Wireshark to take a closer look. When using the Apache telnet client, packets do not contain non-ASCII characters at all, and while using default Ubuntu telnet they do contain them: Apache telnet Default ubuntu telnet
I've been wondering if the Apache client turns on some mode for the server to send only characters that can be encoded on 7 bits. I've tried several terminal types or to subnegotiate binary transmission but without success:
int[] msg = {TelnetCommand.DO,TelnetOption.BINARY};
telnetClient.sendSubnegotiation(msg);
I'm not sure why sub negotiation does not work but I found a workaround.
Try to add SimpleOptionHandler with option 0 (BINARY) to TelnetConnection. It will cause that this option will be enabled from the start.
TelnetClient tc = new TelnetClient();
// 1st param 0 means BINARY option
SimpleOptionHandler simpleOptionHandler = new SimpleOptionHandler(0, true, false, true, false);
tc.addOptionHandler(ttopt);