简体   繁体   English

Apache Telnet 客户端不接收非 ASCII 字符

[英]Apache Telnet Client does not recevie non-ASCII characters

I've been trying to use org.apache.commons.net.telnet.TelnetClient and I am having trouble to receive non-ASCII characters (in my case polish chars like ą,ę,ć,ź and few others).我一直在尝试使用org.apache.commons.net.telnet.TelnetClient并且我无法接收非 ASCII 字符(在我的情况下是波兰字符,如 ą、ę、ć、ź 和其他几个字符)。 The problem is not on server side - when I use default Ubuntu telnet implementation or Putty I have no problem receiving non-ASCII characters.问题不在服务器端——当我使用默认的 Ubuntu telnet 实现或 Putty 时,接收非 ASCII 字符没有问题。 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.我使用 Wireshark 仔细观察。 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 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.我一直想知道 Apache 客户端是否打开了某种模式以使服务器仅发送可以编码为 7 位的字符。 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.尝试将带有选项 0 (BINARY) 的 SimpleOptionHandler 添加到 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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM