java - Perl Pipe 不重定向 Java 进程输出

标签 java perl sockets pipe minecraft

我正在尝试控制游戏服务器并实时显示其输出。这是我到目前为止所拥有的:

#!/usr/bin/perl -w
 use IO::Socket;
 use Net::hostent;              # for OO version of gethostbyaddr

 $PORT = 9000;                  # pick something not in use

 $server = IO::Socket::INET->new( Proto     => 'tcp',
                                  LocalPort => $PORT,
                                  Listen    => SOMAXCONN,
                                  Reuse     => 1);

 die "can't setup server" unless $server;
 print "[Server $0 accepting clients]\n";

 while ($client = $server->accept()) {
   $client->autoflush(1);
   print $client "Welcome to $0; type help for command list.\n";
   $hostinfo = gethostbyaddr($client->peeraddr);
   printf "[Connect from %s]\n", $hostinfo->name || $client->peerhost;
   print $client "Command? ";

   while ( <$client>) {
     next unless /\S/;       # blank line
     if    (/quit|exit/i) {
        last;                                     }
     elsif (/fail|omg/i) {
        printf $client "%s\n", scalar localtime;  }
     elsif (/start/i ) {
        if (my $ping_pid = open(JAVA, "screen java -jar craftbukkit-0.0.1-SNAPSHOT.jar |")) {
    while (my $ping_output = <JAVA>) {
        # Do something with the output, let's say print
        print $client $ping_output;

        # Kill the C program based on some arbitrary condition (in this case
        # the output of the program itself).
                }
        }
        printf $client "I think it started...\n Say status for output\n";                }
     elsif (/stop/i ) {
        print RSPS "stop";

    close(RSPS);
        print  $client "Should be closed.\n"; }
     elsif (/status/i ) {
        $output = RSPS;
        print $client $output;      }
     else {
       print $client "FAIL\n";
     }
   } continue {
      print $client "Command? ";
   }
   close $client;
 }

它启动进程很好,唯一的缺陷是它没有将 Java 进程的输出输出到套接字(它在启动 Perl 的终端窗口中显示输出)我已经用 ping 尝试过,它工作得很好,有什么想法吗?

提前致谢!

最佳答案

听起来 Java 代码(或者可能是 screen )正在将一些输出打印到标准错误流。假设您不想capture it separately ,一些简单的修复是:

抑制它:

open(JAVA, "screen java -jar craftbukkit-0.0.1-SNAPSHOT.jar <b>2>/dev/null</b> |")

在标准输出流中捕获它:

open(JAVA, "screen java -jar craftbukkit-0.0.1-SNAPSHOT.jar <b>2>&1</b> |")

关于java - Perl Pipe 不重定向 Java 进程输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5239596/

相关文章:

java - 为什么 javax.ws.rs.core.UriBuilder、fromUri() 方法在主机名 URI 末尾添加额外的 "/"?

java - 如何在枚举中使用构造函数?

java - PyCharm 教育版无法在 Mac 上启动

class - Perl 中的对象属性访问

c# - 系统编程语言和应用程序编程语言之间的区别

c - "no newline at end of file"C

java - 我正在尝试从类(Starting.java)打开一个类(FlappyBird.java),但我无法使用 "new game().setvisible(true)"

perl - 为什么将 Perl 哈希值添加为键?

c - 将 HTTP GET 响应与传入文件分开

c++ linux套接字服务器反复调用客户端