第一次读取 BufferedReader 流时产生的垃圾

发布于 2024-11-28 04:24:23 字数 729 浏览 0 评论 0原文

我正在构建一个简单的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

清风不识月 2024-12-05 04:24:23

我强烈怀疑这是 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文