title: java simple telnet client using sockets
body: <p>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...</p>
  
  <p>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.</p>
  
  <p>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!</p>
  
  <pre><code>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();
  </code></pre>