使用套接字的java简单telnet客户端

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6399557/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 05:36:27  来源:igfitidea点击:

java simple telnet client using sockets

javasocketstelnet

提问by Oscar Wahltinez

I have read plenty of things on the topic, how telnet is a protocol, not a simple socket connection, waiting for newline characters, use of external libraries and whatnot...

我已经阅读了很多关于该主题的内容,telnet 是一种协议,而不是简单的套接字连接,等待换行符,使用外部库等等......

The bottom line is that I need a quick and dirty java telnet application up and running, not necessarily scalable and not necessarily pretty, so I'm trying to avoid the use of libraries, system function calls and the like. I have been trying and testing and so far, when trying to log into a router (through telnet of course) I have got... nothing.

最重要的是,我需要一个快速而肮脏的 java telnet 应用程序启动并运行,不一定是可扩展的,也不一定是漂亮的,所以我试图避免使用库、系统函数调用等。我一直在尝试和测试,到目前为止,当我尝试登录路由器(当然是通过 telnet)时,我什么都没有。

Here is a snipped of the code that I have been using so far, please someone point me at the right direction because I don't know what else I should try, because I'm certain that it has to be something really simple and silly that I'm missing. Thanks in advance!

这是迄今为止我一直在使用的代码片段,请有人指出正确的方向,因为我不知道我还应该尝试什么,因为我确信它必须非常简单和愚蠢我失踪了。提前致谢!

Socket socket = new Socket("192.168.1.1", 23);
socket.setKeepAlive(true);
BufferedReader r = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter w = new PrintWriter(socket.getOutputStream(),true);

int c=0;
while ((c = r.read()) != -1)
    System.out.print((char)c);

w.print("1234\r\n"); // also tried simply \n or \r
//w.flush();
//Thread.sleep(1000);

while ((c = r.read()) != -1)
    System.out.print((char)c);

w.print("1234\r\n");
//Thread.sleep(1000);

while ((c = r.read()) != -1)
    System.out.print((char)c);

socket.close();

采纳答案by jontro

It's hard to know what's wrong with your example without testing against your particular router. It might be a good idea to use a library, for instance http://sadun-util.sourceforge.net/telnet_library.htmllooks like an easy one to use.

如果不对您的特定路由器进行测试,很难知道您的示例有什么问题。使用库可能是个好主意,例如http://sadun-util.sourceforge.net/telnet_library.html看起来很容易使用。

Also this site says the following:

该网站还说以下内容:

In order to carry on the conversation, a command is issued by simply sending it on the socket's outputstream (and using the telnet newline sequence \r\n):

为了继续对话,只需将命令发送到套接字的输出流(并使用 telnet 换行序列 \r\n)即可发出命令:

 String command="print hello"; 
 PrintWriter pw = new PrintWriter(
      new OutputStreamWriter(s.getOutputStream()), true);
 pw.print(command+"\r\n");

If the session appears to hang after login, avoid to wrap the StreamWriter into a PrintWriter and instead run an explicit flush() at the end:

如果登录后会话似乎挂起,请避免将 StreamWriter 包装到 PrintWriter 中,而是在最后运行显式flush():

 Writer w = new OutputStreamWriter(s.getOutputStream());
 w.print(command+"\r\n");
 w.flush();

This might actually be the problem with your code.

这实际上可能是您的代码的问题。