title: Java and telnet - java.net.SocketException: Broken pipe
body: <p>I have an osgi framework and I want to connect to it via telnet in oder to send only one command - shutdown. That's why I don't want to use telnet libs like apache commons telnet.
  My code:</p>
  
  <pre><code>System.out.println("I am stopping....");
  Socket socket=new Socket("localhost",6666);
  String command="shutdown";
  PrintWriter pw = new PrintWriter( socket.getOutputStream(), true);
  pw.print(command);
  pw.flush();
  socket.close();
  pw.close();
  </code></pre>
  
  <p>It shutdowns osgi but on the side of osgi framework I get:</p>
  
  <pre><code>org.apache.felix.shell.remote [27] TerminalPrintStream::print()
  java.net.SocketException: Broken pipe
      at java.net.SocketOutputStream.socketWrite0(Native Method)
      at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:109)
      at java.net.SocketOutputStream.write(SocketOutputStream.java:153)
      at org.apache.felix.shell.remote.TerminalPrintStream.print(TerminalPrintStream.java:48)
      at org.apache.felix.shell.remote.TerminalPrintStream.println(TerminalPrintStream.java:63)
      at org.apache.felix.shell.remote.Shell.startFelixShell(Shell.java:130)
      at org.apache.felix.shell.remote.Shell.run(Shell.java:86)
      at java.lang.Thread.run(Thread.java:745)
  </code></pre>
  
  <p>How to fix it?</p>
  
  <p><strong>EDIT 1:<br></strong>
  However the following code works without any exception (apache commons net).</p>
  
  <pre><code>TelnetClient telnet=new TelnetClient();
          try {
              telnet.connect("localhost", 6666);
              BufferedInputStream input = new BufferedInputStream(telnet.getInputStream());  
              PrintStream output = new PrintStream(telnet.getOutputStream());  
              output.println("shutdown");
              output.flush();
          } catch (IOException ex) {
              Logger.getLogger(ProgramManager.class.getName()).log(Level.SEVERE, null, ex);
          }
  </code></pre>