第一次读取 BufferedReader 流时产生的垃圾
我正在构建一个简单的 telnet 连接守护进程,用于内部网络应用程序之间的通信,在从 BufferedReader 读取第一行时遇到了问题。
该代码片段并不完整,因为其中还有很多其他垃圾,因此我将其删除,仅包含对象创建和从 steam 读取的内容。
in = new BufferedReader(new InputStreamReader(this.client.getInputStream()));
out = new PrintWriter(this.client.getOutputStream(), true);
String line;
while (true) {
out.println(flag); // flag is just an integer
System.out.println(line);
// Processing the line and updating 'flag' accordingly
}
在 telnet 连接中输入 test
会产生 v? v v? v' ²? v? ²?test
正在运行该程序的控制台。在第一行之后发送的行不会发生这种情况。
有没有办法在用户与它交互之前清除这些垃圾,这样它就不会随第一行一起发送?或者这个问题是由我的 telnet 客户端引起的(当我编写与之交互的客户端时可能会修复)?
I am building a simple telnet connection daemon for communications between internal network applications, and I ran into an issue when reading the first line from BufferedReader.
This code snippet is not complete due to the fact there is a lot of other junk in there so I have stripped it down only to include the object creation and read from the steam.
in = new BufferedReader(new InputStreamReader(this.client.getInputStream()));
out = new PrintWriter(this.client.getOutputStream(), true);
String line;
while (true) {
out.println(flag); // flag is just an integer
System.out.println(line);
// Processing the line and updating 'flag' accordingly
}
Entering test
into the telnet connection yielded v? v v? v' ²? v? ²?test
in the console that was running the program. This does not happen to lines sent after the first one.
Is there a way to clear that garbage out before the user interfaces with it so it doesn't get sent with the first line? or is this issue caused by my telnet client (and might be fixed when I write a client that interfaces with this)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我强烈怀疑这是 telnet 协议协商。理想情况下,您应该仔细阅读 RFC 854 来处理它。
请注意,您不应该只使用
InputStreamReader
而不指定字符编码 - 平台默认编码不太可能是您想要的编码。I strongly suspect it's the telnet protocol negotiation. Ideally, you should handle it having read RFC 854 carefully.
Note that you shouldn't just use
InputStreamReader
without specifying the character encoding - it's very unlikely that the platform default encoding is the one you want.