title: Issue with Socket Streams in Java "Telnet" Code?
body: <p>I'm having trouble transitioning to Java from C/C++ for my "Telnet" interface to some modules we work with here.  I want to be able to establish a connection with a card that, after starting it's command line interface, waits for a connection and serves up a prompt ("OK>") to the clients.  This works fine for both C and C# clients I've written, but the Java has given me some issues.  I've attached some code that I grabbed from some examples online, but so far, all I can ascertain for sure is that the socket is being created.</p>
  
  <p>Code:</p>
  
  <pre><code>   private boolean CreateTelnetSession()
     {
          try
          {
              _socket = new Socket();
              _socket.connect(new InetSocketAddress(_ipAddr, _ipPort));
              _socket.setSoTimeout(10000);
              _socket.setKeepAlive(true);
  
              _out = new PrintWriter(_socket.getOutputStream(), true);
              _in = new BufferedReader(new InputStreamReader(_socket.getInputStream()));
  
              _out.println("\r\n");
  
              System.out.println(_in.readLine());
  
              return true;
          }
          catch(Exception e)
          {
              System.out.println("Exception!");
          }
          return false;
      }
  </code></pre>
  
  <p>The socket SEEMS to be created correctly, and when the program shuts down, I can see the session close on the card(s) I'm trying to talk to, but I don't see the carriage return/line feed echoed on the card as I would expect, or a prompt returned via the InputStream.  Is it possible that it's a character encoding issue?  Am I doing something incorrectly with the streams (crossing them!?!)?  Any insight at all?  When I get over this initial learning curve, I would like to acknowledge how easy Java makes these socket reads and writes, but until then...</p>
  
  <p>I read this post:
  <a href="https://stackoverflow.com/questions/6399557/java-simple-telnet-client-using-sockets">java simple telnet client using sockets</a></p>
  
  <p>It seems similar to what I'm running up against, but it's not the same.  I'm willing to take the rep hit if someone has seen something on here that resolves my issue, so feel free to let me know, bluntly, what I missed.</p>
  
  <p><strong>Edit:</strong></p>
  
  <pre><code>private boolean CreateTelnetSession()
  {
      try
      {
          _socket = new Socket();
          _socket.connect(new InetSocketAddress(_ipAddr, _ipPort));
          _socket.setSoTimeout(10000);
          _socket.setKeepAlive(true);
  
          _out = new DataOutputStream(_socket.getOutputStream());
          _in = new DataInputStream(_socket.getInputStream());
  
          _outBuffer = ByteBuffer.allocate(2048);
          _outBuffer.order(ByteOrder.LITTLE_ENDIAN);
          _inBuffer = ByteBuffer.allocate(2048);
          _inBuffer.order(ByteOrder.LITTLE_ENDIAN);
  
          System.out.println("Connection Response: " + _in.read(_inBuffer.array()));
          System.out.println("Response: " + WriteCommand("DRS\r\n"));
  
          return true;
      }
      catch(Exception e)
      {
          System.out.println("Exception!");
      }
      return false;
  }
  
  private String WriteCommand(String command)
  {
      try
      {
          _outBuffer = encoder.encode(CharBuffer.wrap(command));
          _out.write(_outBuffer.array());
          _out.flush();
  
          _in.read(_inBuffer.array());
          String retString = decoder.decode(_inBuffer).toString();
  
          return retString.substring(0, retString.indexOf('&gt;') + 1);
      }
      catch(Exception e)
      {
          System.out.println("Exception!");
      }
  
      return "E1&gt;";
  }   
  </code></pre>
  
  <p>There are many things to clean up and I'm going to experiment with whether I need to do it in quite this way, but this is the gist of the "solution".  The big killer was the endian-ness.  It should be mentioned, once again, that this is ugly and non-production code, but any other input would still be appreciated.</p>