title: Java socket connection
body: <p>I've a target machine(*nix based) which is connected to my machine(local to my pc). I want to execute some commands on this target.</p>
  
  <p>So, I'm using Java socket to achieve the same.</p>
  
  <pre><code>socket = new Socket("100.200.400.300", 23, null, 0 );
                           remote            local 
  </code></pre>
  
  <p>Here, after the above line -</p>
  
  <pre><code>socket.isConnected() returns true.
  </code></pre>
  
  <p>Now, I've I/O streams of this Socket object and I'm doing -</p>
  
  <pre><code>while((string = bufferedReader.readLine()) != null) {
       outputStream.write(string .getBytes());
       outputStream.flush(); }
  </code></pre>
  
  <p>Buffered reader to read commands from a local file and write to the socket object for execution on the target machine. Below code to read from Socket - </p>
  
  <pre><code>while((myIntVar = is.read()) != -1) {
  //Below line prints some junk data ... hash, updaward arrow and spaces and then
  // loop hangs to raise a Socket I/O exception.
  System.out.println((char) i);
  stringBuffer.append((char) i);}
  </code></pre>
  
  <p>Here, my understanding is that, as I already have the socket connection established, I can just pass my commands and those commands should get executed on the other side(correct me if am wrong).</p>
  
  <p>But this is not working. I'm getting junk characters as I've mentioned above and there is one more thing - I'm not passing username and password for establishing the socket connection - do I've to pass it as we do for telnet(how...? am lost here).</p>
  
  <p>And, just for info - the above code is all that I've(no server or client code as mentioned in various other threads) .</p>