Java simple telnet client using socket

I have read a lot about this topic. Telnet is a protocol, not a simple socket connection, waiting for line breaks, using external libraries and so on

Most importantly, I need a fast and dirty Java telnet application to start and run. It is not necessarily extensible or beautiful, so I try to avoid using libraries, system function calls, etc I've been trying and testing. So far, when I try to log in to the router (via Telnet, of course), I get... No

This is a clip of the code I have used so far. Please point out my right direction, because I don't know what else to try, because I'm sure it must be a very simple and stupid thing. I'm missing Thank you 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();

Solution

If you don't test your specific router, it's hard to know what's wrong with your example Using libraries may be a good idea, for example http://sadun-util.sourceforge.net/telnet_library.html It looks easy to use

The website also says as follows:

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

This may actually be a problem with your code

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>