无需按 Enter 即可通过 telnet 发送数据 [英] Send data over telnet without pressing enter

查看:42
本文介绍了无需按 Enter 即可通过 telnet 发送数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近开始玩弄 Java 套接字和 telnet...

I've recently started messing around with Java sockets and telnet...

我希望用户能够连接到服务器,只需键入一个字母并将其发送到服务器,而无需按 Enter 键发送.我确定服务器无法设置它,但也许 telnet 有一个参数或其他东西可以允许这样做?

I want the user to be able to connect to the server, just type a letter and have it sent to the server, without pressing enter to send it. I'm sure there's no way for the server to set this up, but maybe telnet has a parameter or something which could allow this?

也许如果我让用户在运行 telnet 之前输入 stty cbreakstty raw,这会起作用吗?(仅限 UNIX,我知道!)

Maybe if I got the user to type stty cbreak or stty raw before running telnet, this would work? (UNIX only, I know!)

如果我可以使用 telnet 来执行此操作,那么我就不必为此功能编写特殊的客户端了...

If I can get telnet to do this then it saves me having to write a special client just for this feature...

推荐答案

您应该能够通过 telnet 选项协商来做到这一点.协议默认为半双工模式,至少对于交互会话,服务器应协商抑制继续选项回声选项.

You should be able to do this with telnet option negotiation. The protocol defaults to half-duplex mode, and at a minimum for an interactive session, the server should negotiate the suppress go ahead option and echo option.

至少你可以在开始时吐出ff fb 01 ff fb 03(会回声,会抑制继续前进)会话,然后用 ff fb 01 回复任何 ff fd 01(做回声)(将回显)并回复任何 ff fd 03(禁止前进)与 ff fb 03 (将禁止前进).

At the bare minimum you could just spit out ff fb 01 ff fb 03 (will echo, will suppress-go-ahead) at the begining of the session, then reply to any ff fd 01 (do echo) with ff fb 01 (will echo) and reply to any ff fd 03 (do suppress-go-ahead) with ff fb 03 (will suppress-go-ahead).

编辑补充说,Ben Jackson 提到的线路模式协商是一个更好的答案.对于大多数连接到 23 端口以外的端口的客户端来说,抑制继续是不够的.

Edit to add that the linemode negotiation mentioned by Ben Jackson is a better answer. Suppress go-ahead won't be enough for most clients connecting on ports other than 23.

但是我认为您遇到的另一个问题是 Java 正在发送 Unicode 字符.例如,当您说 (char)0xff 时,Java 假定您指的是 UTF-16 字符 U+00ff,即 ÿ.它可能使用 UTF-8 编码通过套接字发送它,因此 telnet 客户端看到两个字节:c3 bf,它传递并显示为 ÿ.

However I think the other problem you're running into is that Java is sending Unicode characters. For example, when you say (char)0xff, Java assumes you're referring to UTF-16 character U+00ff which is ÿ. It's probably sending it over the socket using UTF-8 encoding, so the telnet client sees two bytes: c3 bf which it passes on and displays as ÿ.

您可以做的是明确告诉 Java 使用 ISO-8859-1 编码.例如,您之前可能一直在做这样的事情:

What you can do is explicitly tell Java to use ISO-8859-1 encoding. For example, you may have been doing something like this before:

out = new PrintStream(connection.getOutputStream());
out.print((char)0xff);  // sends 0xc3 0xbf
out.print((char)0xfb);  // sends 0xc3 0xbb
out.print((char)0x01);  // sends 0x01
out.flush();

相反,您可以使用 OutputStreamWriter 来指定您想要的编码:

Instead, you can use the OutputStreamWriter to specify the encoding you want:

out = new OutputStreamWriter(connection.getOutputStream(), "ISO-8859-1");
out.write((char)0xff);  // sends 0xff
out.write((char)0xfb);  // sends 0xfb
out.write((char)0x01);  // sends 0x01
out.flush();

这篇关于无需按 Enter 即可通过 telnet 发送数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆