为什么套接字连接到端口 23 不起作用?
我有这个小的测试套接字连接类:-
import java.net.Socket;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.IOException;
public class TestTelnet {
public static void main(String args[]) throws Exception {
Telnet telnet = new Telnet();
Socket socket = null ;
socket = new Socket("localhost", 23);
socket.setKeepAlive(true);
BufferedReader r = new BufferedReader(new InputStreamReader(socket.getInputStream()));
BufferedWriter w = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
System.out.println(r.readLine());
socket.close();
}
}
当我使用另一个端口(例如 SMTP 的 25)时它工作得很好,并且 r.readLine 的 println 工作得非常好。 我还可以通过命令提示符 (telnet localhost 23) 连接到端口 23,并得到以下返回信息:-
Ubuntu 8.10 my-laptop 登录:
但是当我尝试使用 java 类连接到端口 23 时,它只是挂在 readLine println 上。 有人知道为什么吗?
I have this small test socket connection class:-
import java.net.Socket;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.IOException;
public class TestTelnet {
public static void main(String args[]) throws Exception {
Telnet telnet = new Telnet();
Socket socket = null ;
socket = new Socket("localhost", 23);
socket.setKeepAlive(true);
BufferedReader r = new BufferedReader(new InputStreamReader(socket.getInputStream()));
BufferedWriter w = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
System.out.println(r.readLine());
socket.close();
}
}
It works perfectly well when I use another port (for example 25 for SMTP) and the println of r.readLine works brilliantly. I can also connect to port 23 via the command prompt (telnet localhost 23) and I get the following returned:-
Ubuntu 8.10
my-laptop login:
But when I try and connect to port 23 using my java class, it just hangs on the readLine println. Does anyone know why this is?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我猜这是因为您期待一行(带有 CR 或 CRLF 终止),但您的 telnet 服务没有发送完整的行。 尝试使用 r.read() 而不是 r.readLine()
I guess it's because your expecting a line (with CR or CRLF termination) but your telnet service does not send a complete line. Try using r.read() instead of r.readLine()
Telnet 是一种协议,可能希望您进行选项协商。 一旦你给它发送了一些有用的东西,它可能也会给你发送一些有用的东西。
请参阅:http://www.faqs.org/rfcs/rfc854.html
Telnet is a protocol, and is probably expecting you to do option negotiation. Once you send it something useful, it will probably send you something useful.
see: http://www.faqs.org/rfcs/rfc854.html
Telnet 既是协议又是应用程序
当您使用 telnet 应用程序连接到计算机时,应用程序会发送协议信息,以便远程计算机可以做出响应。
既然你说它留在那里,那就意味着它正在工作,只是你没有遵循协议。
该协议的描述如下:
https://www.rfc-editor.org/rfc/rfc854< /a>
您在这里尝试编写一个 telnet 应用程序。
Telnet is both a protocol and an application
When you use telnet the application to a machine, the application sends the protocol information so the remote machine may respond.
Since you say it stay there, it means it is working, it's just you are not following the protocol.
The protocol is described here:
https://www.rfc-editor.org/rfc/rfc854
What you're trying to do here is write a telnet application.
Telnet 客户端的一个很好的示例可以在 http://www 中找到.java2s.com/Code/Java/Network-Protocol/ExampleofuseofTelnetClient.htm
A good example of a Telnet client can be found at http://www.java2s.com/Code/Java/Network-Protocol/ExampleofuseofTelnetClient.htm