简体   繁体   English

Java和telnet-java.net.SocketException:管道损坏

[英]Java and telnet - java.net.SocketException: Broken pipe

I have an osgi framework and I want to connect to it via telnet in oder to send only one command - shutdown. 我有一个osgi框架,我想在oder中通过telnet连接到它,仅发送一个命令-shutdown。 That's why I don't want to use telnet libs like apache commons telnet. 这就是为什么我不想使用apache commons telnet之类的telnet库。 My 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();

It shutdowns osgi but on the side of osgi framework I get: 它关闭了osgi,但在osgi框架方面,我得到了:

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)

How to fix it? 如何解决?

EDIT 1: 编辑1:
However the following code works without any exception (apache commons net). 但是,以下代码可以正常工作(apache commons net)。

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);
        }

You should not close the stream as soon as you sent the command as in this case the server cannot answer and throw the exception. 发送命令后,您不应立即关闭流,因为在这种情况下,服务器无法应答并引发异常。 You should read the stream of the socket until it is closed by the server. 您应该读取套接字的流,直到服务器关闭它为止。 It will be closed as soon as the remote shell module is stopped. 一旦远程外壳模块停止,它将关闭。

Based on your code: 根据您的代码:

System.out.println("I am stopping....");
String command="shutdown";
try (Socket socket=new Socket("localhost",6666)) {
  OutputStream out = socket.getOutputStream();
  InputStream in = socket.getInputStream();
  out.write(command.getBytes(Charset.defaultCharset()));

  // Wait until server closes the stream. Could be enhanced with some timeout
  BufferedReader reader = new BufferedReader(new InputStreamReader(in, 
      Charset.defaultCharset())));
  String line = reader.readLine();
  while (line != null) {
    System.out.println(line);
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM